removed accumulating duplicate/stale WebSocket entries over time
This commit is contained in:
parent
4ac1bc78ab
commit
534b00aeb8
|
|
@ -69,6 +69,9 @@ public static class WebsocketManager
|
|||
var installationConnection = InstallationConnections[installationId];
|
||||
Console.WriteLine("Update all the connected websockets for installation " + installation.Name);
|
||||
|
||||
// Prune dead/closed connections before sending
|
||||
installationConnection.Connections.RemoveAll(c => c.State != WebSocketState.Open);
|
||||
|
||||
var jsonObject = new
|
||||
{
|
||||
id = installationId,
|
||||
|
|
@ -82,9 +85,7 @@ public static class WebsocketManager
|
|||
|
||||
// Send to all connections concurrently (preserves original fire-and-forget intent),
|
||||
// but isolate failures so one closed socket doesn't affect others or crash the caller.
|
||||
await Task.WhenAll(connections
|
||||
.Where(c => c.State == WebSocketState.Open)
|
||||
.Select(async c =>
|
||||
await Task.WhenAll(connections.Select(async c =>
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
@ -163,7 +164,10 @@ public static class WebsocketManager
|
|||
};
|
||||
}
|
||||
|
||||
if (!InstallationConnections[installationId].Connections.Contains(currentWebSocket))
|
||||
{
|
||||
InstallationConnections[installationId].Connections.Add(currentWebSocket);
|
||||
}
|
||||
|
||||
var jsonObject = new WebsocketMessage
|
||||
{
|
||||
|
|
@ -197,13 +201,9 @@ public static class WebsocketManager
|
|||
lock (InstallationConnections)
|
||||
{
|
||||
//When the front-end terminates the connection, the following code will be executed
|
||||
//Console.WriteLine("The connection has been terminated");
|
||||
foreach (var installationConnection in InstallationConnections)
|
||||
{
|
||||
if (installationConnection.Value.Connections.Contains(currentWebSocket))
|
||||
{
|
||||
installationConnection.Value.Connections.Remove(currentWebSocket);
|
||||
}
|
||||
installationConnection.Value.Connections.RemoveAll(ws => ws == currentWebSocket);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -224,6 +224,15 @@ public static class WebsocketManager
|
|||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("WebSocket error: " + ex.Message);
|
||||
|
||||
// Ensure stale socket is removed even on unexpected errors
|
||||
lock (InstallationConnections)
|
||||
{
|
||||
foreach (var installationConnection in InstallationConnections)
|
||||
{
|
||||
installationConnection.Value.Connections.RemoveAll(ws => ws == currentWebSocket);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue