🐛 Fixes System.NotSupportedException: WebSockets are not supported
This commit is contained in:
parent
84a88222bd
commit
42b5129aa4
@ -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),
|
||||||
@ -20,57 +20,55 @@ public class WebSocketController : ControllerBase
|
|||||||
[SwaggerIgnore]
|
[SwaggerIgnore]
|
||||||
public async Task TheGateway()
|
public async Task TheGateway()
|
||||||
{
|
{
|
||||||
if (HttpContext.WebSockets.IsWebSocketRequest)
|
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||||
|
HttpContext.Items.TryGetValue("CurrentSession", out var currentSessionValue);
|
||||||
|
if (currentUserValue is not Account.Account currentUser ||
|
||||||
|
currentSessionValue is not Auth.Session currentSession)
|
||||||
{
|
{
|
||||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||||
HttpContext.Items.TryGetValue("CurrentSession", out var currentSessionValue);
|
return;
|
||||||
if (currentUserValue is not Account.Account currentUser ||
|
|
||||||
currentSessionValue is not Auth.Session currentSession)
|
|
||||||
{
|
|
||||||
HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var accountId = currentUser.Id;
|
|
||||||
var deviceId = currentSession.Challenge.DeviceId;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(deviceId))
|
|
||||||
{
|
|
||||||
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
|
||||||
var cts = new CancellationTokenSource();
|
|
||||||
var connectionKey = (accountId, deviceId);
|
|
||||||
|
|
||||||
if (!ActiveConnections.TryAdd(connectionKey, (webSocket, cts)))
|
|
||||||
{
|
|
||||||
await webSocket.CloseAsync(
|
|
||||||
WebSocketCloseStatus.InternalServerError,
|
|
||||||
"Failed to establish connection.",
|
|
||||||
CancellationToken.None
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await _ConnectionEventLoop(webSocket, connectionKey, cts.Token);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"WebSocket Error: {ex.Message}");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
ActiveConnections.TryRemove(connectionKey, out _);
|
|
||||||
cts.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
var accountId = currentUser.Id;
|
||||||
|
var deviceId = currentSession.Challenge.DeviceId;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(deviceId))
|
||||||
{
|
{
|
||||||
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
var connectionKey = (accountId, deviceId);
|
||||||
|
|
||||||
|
if (!ActiveConnections.TryAdd(connectionKey, (webSocket, cts)))
|
||||||
|
{
|
||||||
|
await webSocket.CloseAsync(
|
||||||
|
WebSocketCloseStatus.InternalServerError,
|
||||||
|
"Failed to establish connection.",
|
||||||
|
CancellationToken.None
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.LogInformation(
|
||||||
|
$"Connection established with user @{currentUser.Name}#{currentUser.Id} and device #{deviceId}");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _ConnectionEventLoop(webSocket, connectionKey, cts.Token);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"WebSocket Error: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ActiveConnections.TryRemove(connectionKey, out _);
|
||||||
|
cts.Dispose();
|
||||||
|
logger.LogInformation(
|
||||||
|
$"Connection disconnected with user @{currentUser.Name}#{currentUser.Id} and device #{deviceId}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,6 +173,7 @@ app.UseCors(opts =>
|
|||||||
.AllowAnyMethod()
|
.AllowAnyMethod()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.UseWebSockets();
|
||||||
app.UseRateLimiter();
|
app.UseRateLimiter();
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user