Handling Disconnects

Connections can be lost for various reasons due to network issues, or when a client teleports to a different server instance. It is important to understand how to handle disconnects and reconnects to ensure a smooth experience for your users.

Platform Reconnect Logic

If an unhandled disconnect occurs, the client will automatically attempt to reconnect to the same server instance. We retry up to 3 times with an increasing delay between each attempt. If the client fails to reconnect after 3 attempts, the client will leave the Space completely.

Monitoring Connection Status

Connection status can be monitored using INetworkingService.isConnected, INetworkingService.connectionStatus and INetworkingService.onConnectionStatusChanged.

public class GameManager : MonoBehaviour
{
public GameObject loadingScreen;

private void Start()
{
SpatialBridge.networkingService.onConnectionStatusChanged += HandleConnectionStatusChanged;

// Initial refresh of systems
HandleConnectionStatusChanged(SpatialBridge.networkingService.connectionStatus);
}

private void HandleConnectionStatusChanged(ServerConnectionStatus status)
{
bool isConnected = SpatialBridge.networkingService.isConnected;
Debug.Log($"[MatchmakingDemo] GameManager.HandleConnectionStatusChanged: status={status} isConnected={isConnected}");

// Only show the loading screen if we're not connected
loadingScreen.SetActive(!isConnected);
}
}
public class GameManager : MonoBehaviour
{
public GameObject loadingScreen;

private void Start()
{
SpatialBridge.networkingService.onConnectionStatusChanged += HandleConnectionStatusChanged;

// Initial refresh of systems
HandleConnectionStatusChanged(SpatialBridge.networkingService.connectionStatus);
}

private void HandleConnectionStatusChanged(ServerConnectionStatus status)
{
bool isConnected = SpatialBridge.networkingService.isConnected;
Debug.Log($"[MatchmakingDemo] GameManager.HandleConnectionStatusChanged: status={status} isConnected={isConnected}");

// Only show the loading screen if we're not connected
loadingScreen.SetActive(!isConnected);
}
}

Handling Disconnects

When a client disconnects, you may want to listen for this disconnect event and perform some cleanup and teardown of game logic.

While synchronized state (Network Variables) will be reset to it's state on the server on reconnect, any local variables in your C# code will remain as they were. It is your responsibility to reset these variables appropriately. What state needs to be reset will depend on the specifics of your experience.