From 758186f674dc86a47eb4f24662e3b5bc5c4f7ca1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 30 Apr 2025 00:50:58 +0800 Subject: [PATCH] :bug: Fix swaggergen --- DysonNetwork.Sphere/.DS_Store | Bin 0 -> 6148 bytes .../Connection/WebSocketController.cs | 72 ++++++++++++++---- 2 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 DysonNetwork.Sphere/.DS_Store diff --git a/DysonNetwork.Sphere/.DS_Store b/DysonNetwork.Sphere/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b5384573327280595eae406ccf6a65cafa24dc3c GIT binary patch literal 6148 zcmeHKOH0E*5T0$TO({YT3Oz1(E!c+?iboo2w`Z)x57}Nto(f&p4clQyJFki$%NFZOG11 zYuS*CUc1?lhn?O_Eq8_vp#9@@Ph8 zfEi#0n1Rh=z@7t6b+gLmYMB9M;0FxQ{vfdtx)w8odh5WBuFvEz5t5)zZwW$a(Y2Tv zL=TEEsfZ?3*e8ZC>1dZW&b62sH0dDp$~cc*Ie)wey*k>Z4hP{H ActiveConnections = new ConcurrentDictionary<(long, string), (WebSocket, CancellationTokenSource)>(); + private static readonly ConcurrentDictionary< + (long AccountId, string DeviceId), + (WebSocket Socket, CancellationTokenSource Cts) + > ActiveConnections = + new ConcurrentDictionary<(long, string), (WebSocket, CancellationTokenSource)>(); [Route("/ws")] [Authorize] - public async Task TheGateway([FromQuery] string deviceId) + [SwaggerIgnore] + public async Task TheGateway() { if (HttpContext.WebSockets.IsWebSocketRequest) { // Get AccountId from HttpContext - if (!HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue) || currentUserValue is not Account.Account currentUser) + if ( + !HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue) + || currentUserValue is not Account.Account currentUser + ) { HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized; return; @@ -35,7 +44,7 @@ public class WebSocketController : ControllerBase return; } - using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(); + using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(); // Create a CancellationTokenSource for this connection var cts = new CancellationTokenSource(); var connectionKey = (accountId, deviceId); @@ -44,10 +53,14 @@ public class WebSocketController : ControllerBase if (!ActiveConnections.TryAdd(connectionKey, (webSocket, cts))) { // Failed to add - await webSocket.CloseAsync(WebSocketCloseStatus.InternalServerError, "Failed to establish connection.", CancellationToken.None); + await webSocket.CloseAsync( + WebSocketCloseStatus.InternalServerError, + "Failed to establish connection.", + CancellationToken.None + ); return; } - + try { await _ConnectionEventLoop(webSocket, connectionKey, cts.Token); @@ -69,29 +82,50 @@ public class WebSocketController : ControllerBase } } - private static async Task _ConnectionEventLoop(WebSocket webSocket, (long AccountId, string DeviceId) connectionKey, CancellationToken cancellationToken) + private static async Task _ConnectionEventLoop( + WebSocket webSocket, + (long AccountId, string DeviceId) connectionKey, + CancellationToken cancellationToken + ) { // Buffer for receiving messages. - var buffer = new byte[1024 * 4]; + var buffer = new byte[1024 * 4]; try { // We don't handle receiving data, so we ignore the return. - var receiveResult = await webSocket.ReceiveAsync(new ArraySegment(buffer), cancellationToken); + var receiveResult = await webSocket.ReceiveAsync( + new ArraySegment(buffer), + cancellationToken + ); while (!receiveResult.CloseStatus.HasValue) { // Keep connection alive and wait for close requests - receiveResult = await webSocket.ReceiveAsync(new ArraySegment(buffer), cancellationToken); + receiveResult = await webSocket.ReceiveAsync( + new ArraySegment(buffer), + cancellationToken + ); } // Close connection - await webSocket.CloseAsync(receiveResult.CloseStatus.Value, receiveResult.CloseStatusDescription, cancellationToken); + await webSocket.CloseAsync( + receiveResult.CloseStatus.Value, + receiveResult.CloseStatusDescription, + cancellationToken + ); } catch (OperationCanceledException) { // Connection was canceled, close it gracefully - if (webSocket.State != WebSocketState.Closed && webSocket.State != WebSocketState.Aborted) + if ( + webSocket.State != WebSocketState.Closed + && webSocket.State != WebSocketState.Aborted + ) { - await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Connection closed by server", CancellationToken.None); + await webSocket.CloseAsync( + WebSocketCloseStatus.NormalClosure, + "Connection closed by server", + CancellationToken.None + ); } } } @@ -102,7 +136,13 @@ public class WebSocketController : ControllerBase if (ActiveConnections.TryGetValue((accountId, deviceId), out var connection)) { var buffer = System.Text.Encoding.UTF8.GetBytes(message); - await connection.Socket.SendAsync(new ArraySegment(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, connection.Cts.Token); + await connection.Socket.SendAsync( + new ArraySegment(buffer, 0, buffer.Length), + WebSocketMessageType.Text, + true, + connection.Cts.Token + ); } } -} \ No newline at end of file +} +