From 0bb78dd27d855ea50e625ae91be98c99e037d422 Mon Sep 17 00:00:00 2001 From: John Detter Date: Sat, 12 Oct 2024 07:15:28 -0500 Subject: [PATCH] Disconnected connection should be removed from active connections Small fix that has to do with the active connections list being modified during enumeration Small fix Another fix Small simplification, should be working now Small improvement to how OnDestroy works Small fix Compilation fix :shrug-emoji: --- src/SpacetimeDBClient.cs | 9 ++++++++- src/SpacetimeDBNetworkManager.cs | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/SpacetimeDBClient.cs b/src/SpacetimeDBClient.cs index e076fa49..4e5dc2c7 100644 --- a/src/SpacetimeDBClient.cs +++ b/src/SpacetimeDBClient.cs @@ -36,7 +36,10 @@ public DbConnection Build() } conn.Connect(token, uri, nameOrAddress, compression ?? Compression.Brotli); #if UNITY_5_3_OR_NEWER - SpacetimeDBNetworkManager.ActiveConnections.Add(conn); + if (!SpacetimeDBNetworkManager.ActiveConnections.Contains(conn)) + { + SpacetimeDBNetworkManager.ActiveConnections.Add(conn); + } #endif return conn; } @@ -584,6 +587,10 @@ public void Disconnect() connectionClosed = true; webSocket.Close(); _preProcessCancellationTokenSource.Cancel(); + +#if UNITY_5_3_OR_NEWER + SpacetimeDBNetworkManager.ActiveConnections.Remove(this); +#endif } /// diff --git a/src/SpacetimeDBNetworkManager.cs b/src/SpacetimeDBNetworkManager.cs index 87817864..6d0df367 100644 --- a/src/SpacetimeDBNetworkManager.cs +++ b/src/SpacetimeDBNetworkManager.cs @@ -1,6 +1,7 @@ #if UNITY_5_3_OR_NEWER using System; using System.Collections.Generic; +using System.Linq; using SpacetimeDB; using UnityEngine; @@ -37,7 +38,14 @@ private void ForEachConnection(Action action) } private void Update() => ForEachConnection(conn => conn.FrameTick()); - private void OnDestroy() => ForEachConnection(conn => conn.Disconnect()); + private void OnDestroy() + { + // Disconnecting this connection will cause it to remove itself from ActiveConnections + foreach(var conn in ActiveConnections.ToList()) + { + conn.Disconnect(); + } + } } } #endif