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];
|
var installationConnection = InstallationConnections[installationId];
|
||||||
Console.WriteLine("Update all the connected websockets for installation " + installation.Name);
|
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
|
var jsonObject = new
|
||||||
{
|
{
|
||||||
id = installationId,
|
id = installationId,
|
||||||
|
|
@ -82,20 +85,18 @@ public static class WebsocketManager
|
||||||
|
|
||||||
// Send to all connections concurrently (preserves original fire-and-forget intent),
|
// 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.
|
// but isolate failures so one closed socket doesn't affect others or crash the caller.
|
||||||
await Task.WhenAll(connections
|
await Task.WhenAll(connections.Select(async c =>
|
||||||
.Where(c => c.State == WebSocketState.Open)
|
{
|
||||||
.Select(async c =>
|
try
|
||||||
{
|
{
|
||||||
try
|
await c.SendAsync(new ArraySegment<byte>(dataToSend, 0, dataToSend.Length),
|
||||||
{
|
WebSocketMessageType.Text, true, CancellationToken.None);
|
||||||
await c.SendAsync(new ArraySegment<byte>(dataToSend, 0, dataToSend.Length),
|
}
|
||||||
WebSocketMessageType.Text, true, CancellationToken.None);
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
Console.WriteLine($"WebSocket send failed for installation {installationId}: {ex.Message}");
|
||||||
{
|
}
|
||||||
Console.WriteLine($"WebSocket send failed for installation {installationId}: {ex.Message}");
|
}));
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -163,7 +164,10 @@ public static class WebsocketManager
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
InstallationConnections[installationId].Connections.Add(currentWebSocket);
|
if (!InstallationConnections[installationId].Connections.Contains(currentWebSocket))
|
||||||
|
{
|
||||||
|
InstallationConnections[installationId].Connections.Add(currentWebSocket);
|
||||||
|
}
|
||||||
|
|
||||||
var jsonObject = new WebsocketMessage
|
var jsonObject = new WebsocketMessage
|
||||||
{
|
{
|
||||||
|
|
@ -197,13 +201,9 @@ public static class WebsocketManager
|
||||||
lock (InstallationConnections)
|
lock (InstallationConnections)
|
||||||
{
|
{
|
||||||
//When the front-end terminates the connection, the following code will be executed
|
//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)
|
foreach (var installationConnection in InstallationConnections)
|
||||||
{
|
{
|
||||||
if (installationConnection.Value.Connections.Contains(currentWebSocket))
|
installationConnection.Value.Connections.RemoveAll(ws => ws == currentWebSocket);
|
||||||
{
|
|
||||||
installationConnection.Value.Connections.Remove(currentWebSocket);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -224,6 +224,15 @@ public static class WebsocketManager
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine("WebSocket error: " + ex.Message);
|
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