🐛 Fixes System.NotSupportedException: WebSockets are not supported

This commit is contained in:
LittleSheep 2025-05-01 01:22:02 +08:00
parent 84a88222bd
commit 42b5129aa4
2 changed files with 47 additions and 48 deletions

View File

@ -8,7 +8,7 @@ namespace DysonNetwork.Sphere.Connection;
[ApiController] [ApiController]
[Route("/ws")] [Route("/ws")]
public class WebSocketController : ControllerBase public class WebSocketController(ILogger<WebSocketContext> logger) : ControllerBase
{ {
private static readonly ConcurrentDictionary< private static readonly ConcurrentDictionary<
(long AccountId, string DeviceId), (long AccountId, string DeviceId),
@ -19,8 +19,6 @@ public class WebSocketController : ControllerBase
[Authorize] [Authorize]
[SwaggerIgnore] [SwaggerIgnore]
public async Task TheGateway() public async Task TheGateway()
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{ {
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue); HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
HttpContext.Items.TryGetValue("CurrentSession", out var currentSessionValue); HttpContext.Items.TryGetValue("CurrentSession", out var currentSessionValue);
@ -54,6 +52,9 @@ public class WebSocketController : ControllerBase
return; return;
} }
logger.LogInformation(
$"Connection established with user @{currentUser.Name}#{currentUser.Id} and device #{deviceId}");
try try
{ {
await _ConnectionEventLoop(webSocket, connectionKey, cts.Token); await _ConnectionEventLoop(webSocket, connectionKey, cts.Token);
@ -66,11 +67,8 @@ public class WebSocketController : ControllerBase
{ {
ActiveConnections.TryRemove(connectionKey, out _); ActiveConnections.TryRemove(connectionKey, out _);
cts.Dispose(); cts.Dispose();
} logger.LogInformation(
} $"Connection disconnected with user @{currentUser.Name}#{currentUser.Id} and device #{deviceId}");
else
{
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
} }
} }

View File

@ -173,6 +173,7 @@ app.UseCors(opts =>
.AllowAnyMethod() .AllowAnyMethod()
); );
app.UseWebSockets();
app.UseRateLimiter(); app.UseRateLimiter();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseAuthorization(); app.UseAuthorization();