Skip to content

Commit 07e12d4

Browse files
authored
Merge pull request #6 from Simyon264/remove-web-sockets
Remove web sockets for rework (someday)
2 parents 9457ff0 + b9c5c5c commit 07e12d4

File tree

3 files changed

+3
-106
lines changed

3 files changed

+3
-106
lines changed

Client/Components/App.razor

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,6 @@
2323

2424
<Routes/>
2525
<script src="_framework/blazor.web.js"></script>
26-
<script>
27-
// This script handles the web socket connection to the server.
28-
const websocketUrl = "@Configuration["WebSocketUrl"]";
29-
const ws = new WebSocket(websocketUrl);
30-
ws.onopen = () => {
31-
console.log("WebSocket connection established.");
32-
ws.send("count");
33-
};
34-
ws.onclose = () => {
35-
console.log("WebSocket connection closed.");
36-
};
37-
ws.onerror = (error) => {
38-
console.error("WebSocket error: ", error);
39-
};
40-
ws.onmessage = (message) => {
41-
//console.log("WebSocket message: ", message.data);
42-
// The only message i can currently recieve are the amount of users online. TODO: JSON messages
43-
//if (message.data == "1") {
44-
// $("#activeUsers").html("There is currently <em>one user</em> here.");
45-
//} else {
46-
// $("#activeUsers").html("There are currently <em>" + message.data + " users</em> here.");
47-
//}
48-
};
49-
window.ws = ws;
50-
51-
// Every 5 seconds, send a "ping" message to the server.
52-
setInterval(() => {
53-
if (ws.readyState !== WebSocket.OPEN) {
54-
console.error("WebSocket connection is not open.");
55-
return;
56-
}
57-
ws.send("count");
58-
}, 1500);
59-
60-
// When the user navigates to a new page, close the web socket connection.
61-
window.addEventListener("beforeunload", () => {
62-
ws.close();
63-
});
64-
</script>
6526
</body>
6627

67-
</html>
28+
</html>

Server/Api/DataController.cs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Concurrent;
22
using System.Diagnostics;
3-
using System.Net.WebSockets;
43
using System.Text;
54
using System.Text.Json;
65
using System.Text.RegularExpressions;
@@ -26,13 +25,9 @@ public class DataController : ControllerBase
2625
private readonly ReplayDbContext _context;
2726
private readonly IMemoryCache _cache;
2827

29-
public static readonly Dictionary<Guid, WebSocket> ConnectedUsers = new();
30-
private Timer _timer;
31-
3228
public DataController(ReplayDbContext context, IMemoryCache cache)
3329
{
3430
_context = context;
35-
_timer = new Timer(CheckInactiveConnections, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
3631
_cache = cache;
3732
}
3833

@@ -392,47 +387,6 @@ public async Task<LeaderboardData> GetLeaderboard(
392387

393388
return leaderboardResult;
394389
}
395-
396-
[HttpGet] // this is kind of stupid? swagger does not work without having a method identifier or something
397-
[Route("/ws")]
398-
public async Task Connect()
399-
{
400-
if (HttpContext.WebSockets.IsWebSocketRequest)
401-
{
402-
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
403-
var userId = Guid.NewGuid();
404-
ConnectedUsers.Add(userId, webSocket);
405-
Log.Information("User connected with ID {UserId}", userId);
406-
await Echo(webSocket, userId);
407-
}
408-
else
409-
{
410-
HttpContext.Response.StatusCode = 400;
411-
}
412-
}
413-
414-
private async Task Echo(WebSocket webSocket, Guid userId)
415-
{
416-
var buffer = new byte[1024 * 4];
417-
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
418-
419-
while (!result.CloseStatus.HasValue)
420-
{
421-
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
422-
423-
if (Encoding.UTF8.GetString(buffer).Contains("count"))
424-
{
425-
var count = ConnectedUsers.Count;
426-
var countBytes = Encoding.UTF8.GetBytes(count.ToString());
427-
await webSocket.SendAsync(new ArraySegment<byte>(countBytes), WebSocketMessageType.Text, true, CancellationToken.None);
428-
}
429-
430-
buffer = new byte[1024 * 4];
431-
}
432-
433-
ConnectedUsers.Remove(userId, out _);
434-
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
435-
}
436390

437391
private async Task<PlayerData?> FetchPlayerDataFromGuid(Guid guid)
438392
{
@@ -473,15 +427,4 @@ private async Task Echo(WebSocket webSocket, Guid userId)
473427

474428
return playerKey;
475429
}
476-
477-
private void CheckInactiveConnections(object state)
478-
{
479-
foreach (var user in ConnectedUsers)
480-
{
481-
if (user.Value.State == WebSocketState.Open) continue;
482-
483-
ConnectedUsers.Remove(user.Key, out _);
484-
Log.Information("User disconnected with ID {UserId}", user.Key);
485-
}
486-
}
487-
}
430+
}

Server/Program.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,6 @@
144144
});
145145

146146
var app = builder.Build();
147-
148-
var webSocketOptions = new WebSocketOptions()
149-
{
150-
KeepAliveInterval = TimeSpan.FromSeconds(10),
151-
};
152-
153-
app.UseWebSockets(webSocketOptions);
154147

155148
// Configure the HTTP request pipeline.
156149
if (app.Environment.IsDevelopment())
@@ -198,4 +191,4 @@
198191
finally
199192
{
200193
Log.CloseAndFlush();
201-
}
194+
}

0 commit comments

Comments
 (0)