🐛 Fix swaggergen
This commit is contained in:
parent
3637225d23
commit
758186f674
BIN
DysonNetwork.Sphere/.DS_Store
vendored
Normal file
BIN
DysonNetwork.Sphere/.DS_Store
vendored
Normal file
Binary file not shown.
@ -3,6 +3,7 @@ using System.Net.WebSockets;
|
|||||||
using DysonNetwork.Sphere.Account;
|
using DysonNetwork.Sphere.Account;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Swashbuckle.AspNetCore.Annotations;
|
||||||
|
|
||||||
namespace DysonNetwork.Sphere.Connection;
|
namespace DysonNetwork.Sphere.Connection;
|
||||||
|
|
||||||
@ -12,16 +13,24 @@ public class WebSocketController : ControllerBase
|
|||||||
{
|
{
|
||||||
// Concurrent dictionary to store active WebSocket connections.
|
// Concurrent dictionary to store active WebSocket connections.
|
||||||
// Key: Tuple (AccountId, DeviceId); Value: WebSocket and CancellationTokenSource
|
// Key: Tuple (AccountId, DeviceId); Value: WebSocket and CancellationTokenSource
|
||||||
private static readonly ConcurrentDictionary<(long AccountId, string DeviceId), (WebSocket Socket, CancellationTokenSource Cts)> 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")]
|
[Route("/ws")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task TheGateway([FromQuery] string deviceId)
|
[SwaggerIgnore]
|
||||||
|
public async Task TheGateway()
|
||||||
{
|
{
|
||||||
if (HttpContext.WebSockets.IsWebSocketRequest)
|
if (HttpContext.WebSockets.IsWebSocketRequest)
|
||||||
{
|
{
|
||||||
// Get AccountId from HttpContext
|
// 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;
|
HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||||
return;
|
return;
|
||||||
@ -44,7 +53,11 @@ public class WebSocketController : ControllerBase
|
|||||||
if (!ActiveConnections.TryAdd(connectionKey, (webSocket, cts)))
|
if (!ActiveConnections.TryAdd(connectionKey, (webSocket, cts)))
|
||||||
{
|
{
|
||||||
// Failed to add
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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.
|
// Buffer for receiving messages.
|
||||||
var buffer = new byte[1024 * 4];
|
var buffer = new byte[1024 * 4];
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// We don't handle receiving data, so we ignore the return.
|
// We don't handle receiving data, so we ignore the return.
|
||||||
var receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
|
var receiveResult = await webSocket.ReceiveAsync(
|
||||||
|
new ArraySegment<byte>(buffer),
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
while (!receiveResult.CloseStatus.HasValue)
|
while (!receiveResult.CloseStatus.HasValue)
|
||||||
{
|
{
|
||||||
// Keep connection alive and wait for close requests
|
// Keep connection alive and wait for close requests
|
||||||
receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
|
receiveResult = await webSocket.ReceiveAsync(
|
||||||
|
new ArraySegment<byte>(buffer),
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close connection
|
// Close connection
|
||||||
await webSocket.CloseAsync(receiveResult.CloseStatus.Value, receiveResult.CloseStatusDescription, cancellationToken);
|
await webSocket.CloseAsync(
|
||||||
|
receiveResult.CloseStatus.Value,
|
||||||
|
receiveResult.CloseStatusDescription,
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
// Connection was canceled, close it gracefully
|
// 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))
|
if (ActiveConnections.TryGetValue((accountId, deviceId), out var connection))
|
||||||
{
|
{
|
||||||
var buffer = System.Text.Encoding.UTF8.GetBytes(message);
|
var buffer = System.Text.Encoding.UTF8.GetBytes(message);
|
||||||
await connection.Socket.SendAsync(new ArraySegment<byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, connection.Cts.Token);
|
await connection.Socket.SendAsync(
|
||||||
|
new ArraySegment<byte>(buffer, 0, buffer.Length),
|
||||||
|
WebSocketMessageType.Text,
|
||||||
|
true,
|
||||||
|
connection.Cts.Token
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user