Skip to content

Commit

Permalink
Disconnected connection should be removed from active connections
Browse files Browse the repository at this point in the history
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:
  • Loading branch information
John Detter committed Oct 16, 2024
1 parent 4d7f23c commit 0bb78dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/SpacetimeDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -584,6 +587,10 @@ public void Disconnect()
connectionClosed = true;
webSocket.Close();
_preProcessCancellationTokenSource.Cancel();

#if UNITY_5_3_OR_NEWER
SpacetimeDBNetworkManager.ActiveConnections.Remove(this);
#endif
}

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/SpacetimeDBNetworkManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if UNITY_5_3_OR_NEWER
using System;
using System.Collections.Generic;
using System.Linq;
using SpacetimeDB;
using UnityEngine;

Expand Down Expand Up @@ -37,7 +38,14 @@ private void ForEachConnection(Action<IDbConnection> 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

0 comments on commit 0bb78dd

Please sign in to comment.