1
1
using System . Collections . Concurrent ;
2
2
using System . Diagnostics ;
3
- using System . Net . WebSockets ;
4
3
using System . Text ;
5
4
using System . Text . Json ;
6
5
using System . Text . RegularExpressions ;
@@ -26,13 +25,9 @@ public class DataController : ControllerBase
26
25
private readonly ReplayDbContext _context ;
27
26
private readonly IMemoryCache _cache ;
28
27
29
- public static readonly Dictionary < Guid , WebSocket > ConnectedUsers = new ( ) ;
30
- private Timer _timer ;
31
-
32
28
public DataController ( ReplayDbContext context , IMemoryCache cache )
33
29
{
34
30
_context = context ;
35
- _timer = new Timer ( CheckInactiveConnections , null , TimeSpan . Zero , TimeSpan . FromSeconds ( 5 ) ) ;
36
31
_cache = cache ;
37
32
}
38
33
@@ -392,47 +387,6 @@ public async Task<LeaderboardData> GetLeaderboard(
392
387
393
388
return leaderboardResult ;
394
389
}
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
- }
436
390
437
391
private async Task < PlayerData ? > FetchPlayerDataFromGuid ( Guid guid )
438
392
{
@@ -473,15 +427,4 @@ private async Task Echo(WebSocket webSocket, Guid userId)
473
427
474
428
return playerKey ;
475
429
}
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
+ }
0 commit comments