diff --git a/DysonNetwork.Pusher/Connection/WebSocketController.cs b/DysonNetwork.Pusher/Connection/WebSocketController.cs index 71c41b3..352ad1e 100644 --- a/DysonNetwork.Pusher/Connection/WebSocketController.cs +++ b/DysonNetwork.Pusher/Connection/WebSocketController.cs @@ -32,7 +32,8 @@ public class WebSocketController(WebSocketService ws, ILogger return; } - using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(); + var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(new WebSocketAcceptContext + { KeepAliveInterval = TimeSpan.FromSeconds(60) }); var cts = new CancellationTokenSource(); var connectionKey = (accountId, deviceId); @@ -65,7 +66,8 @@ public class WebSocketController(WebSocketService ws, ILogger } catch (Exception ex) { - logger.LogError(ex, "WebSocket disconnected with user @{UserName}#{UserId} and device #{DeviceId} unexpectedly"); + logger.LogError(ex, + "WebSocket disconnected with user @{UserName}#{UserId} and device #{DeviceId} unexpectedly"); } finally { @@ -99,7 +101,7 @@ public class WebSocketController(WebSocketService ws, ILogger break; var packet = WebSocketPacket.FromBytes(buffer[..receiveResult.Count]); - _ = ws.HandlePacket(currentUser, connectionKey.DeviceId, packet, webSocket); + await ws.HandlePacket(currentUser, connectionKey.DeviceId, packet, webSocket); } } catch (OperationCanceledException) @@ -113,4 +115,4 @@ public class WebSocketController(WebSocketService ws, ILogger } } } -} +} \ No newline at end of file diff --git a/DysonNetwork.Pusher/Connection/WebSocketService.cs b/DysonNetwork.Pusher/Connection/WebSocketService.cs index 40b3522..add7645 100644 --- a/DysonNetwork.Pusher/Connection/WebSocketService.cs +++ b/DysonNetwork.Pusher/Connection/WebSocketService.cs @@ -49,11 +49,18 @@ public class WebSocketService public void Disconnect((string AccountId, string DeviceId) key, string? reason = null) { if (!ActiveConnections.TryGetValue(key, out var data)) return; - data.Socket.CloseAsync( - WebSocketCloseStatus.NormalClosure, - reason ?? "Server just decided to disconnect.", - CancellationToken.None - ); + try + { + data.Socket.CloseAsync( + WebSocketCloseStatus.NormalClosure, + reason ?? "Server just decided to disconnect.", + CancellationToken.None + ); + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Error while closing WebSocket for {AccountId}:{DeviceId}", key.AccountId, key.DeviceId); + } data.Cts.Cancel(); ActiveConnections.TryRemove(key, out _); } diff --git a/DysonNetwork.Pusher/Notification/PushService.cs b/DysonNetwork.Pusher/Notification/PushService.cs index 050ac19..2f31969 100644 --- a/DysonNetwork.Pusher/Notification/PushService.cs +++ b/DysonNetwork.Pusher/Notification/PushService.cs @@ -56,8 +56,6 @@ public class PushService _ws = ws; _queueService = queueService; _logger = logger; - - _logger.LogInformation("PushService initialized"); } public async Task UnsubscribeDevice(string deviceId) diff --git a/DysonNetwork.Pusher/Services/QueueBackgroundService.cs b/DysonNetwork.Pusher/Services/QueueBackgroundService.cs index 5bdda67..422d042 100644 --- a/DysonNetwork.Pusher/Services/QueueBackgroundService.cs +++ b/DysonNetwork.Pusher/Services/QueueBackgroundService.cs @@ -51,7 +51,6 @@ public class QueueBackgroundService( } else { - await msg.ReplyAsync(cancellationToken: stoppingToken); logger.LogWarning($"Invalid message format for {msg.Subject}"); } } @@ -92,8 +91,6 @@ public class QueueBackgroundService( logger.LogWarning("Unknown message type: {MessageType}", message.Type); break; } - - await rawMsg.ReplyAsync(cancellationToken: cancellationToken); } catch (Exception ex) { diff --git a/DysonNetwork.Pusher/Startup/ServiceCollectionExtensions.cs b/DysonNetwork.Pusher/Startup/ServiceCollectionExtensions.cs index ffefb67..75c8dad 100644 --- a/DysonNetwork.Pusher/Startup/ServiceCollectionExtensions.cs +++ b/DysonNetwork.Pusher/Startup/ServiceCollectionExtensions.cs @@ -134,7 +134,7 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddAppBusinessServices(this IServiceCollection services) { - services.AddScoped(); + services.AddSingleton(); services.AddScoped(); services.AddScoped(); diff --git a/DysonNetwork.Shared/Http/KestrelConfiguration.cs b/DysonNetwork.Shared/Http/KestrelConfiguration.cs index a493893..bd6bf1b 100644 --- a/DysonNetwork.Shared/Http/KestrelConfiguration.cs +++ b/DysonNetwork.Shared/Http/KestrelConfiguration.cs @@ -20,9 +20,7 @@ public static class KestrelConfiguration builder.WebHost.ConfigureKestrel(options => { options.Limits.MaxRequestBodySize = maxRequestBodySize; - options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); - options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30); - + var configuredUrl = Environment.GetEnvironmentVariable("ASPNETCORE_URLS"); if (!string.IsNullOrEmpty(configuredUrl)) return; diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index f8a57a7..7042f50 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -146,6 +146,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded <AssemblyExplorer> <Assembly Path="/opt/homebrew/Cellar/dotnet/9.0.6/libexec/packs/Microsoft.AspNetCore.App.Ref/9.0.6/ref/net9.0/Microsoft.AspNetCore.RateLimiting.dll" />