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

View File

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