Photon Connection Lost & Quick Reconnect
Feature Overview
Staring on Fusion 2.0.2, the new Photon Cloud Connection Lost feature introduces a new callback, NetworkRunner.CloudConnectionLost
, to handle Photon Cloud Connection Timeouts more gracefully. This feature allows for better management of network disconnections, providing developers with more control over how their application responds to connection issues related to the Photon Cloud.
This feature applies only to Client/Server modes, so only Host, Server, and Client modes are affected. The feature is not supported on Shared mode.
CloudConnectionLost Callback
Purpose
The NetworkRunner.CloudConnectionLost
callback is designed to handle scenarios where a peer loses connection to the Photon Cloud. Instead of immediately shutting down the session, this feature allows for automatic reconnection attempts and provides developers with the opportunity to implement custom behavior based on the connection status.
Callback Signature
C#
NetworkRunner.CloudConnectionLost += (NetworkRunner runner, ShutdownReason reason, bool reconnecting) => { ... };
runner
: Reference to the NetworkRunner instance.reason
: The ShutdownReason, indicating why the connection was lost.reconnecting
: A boolean flag indicating if a session re-join is in progress.
Triggering Conditions
The callback is fired in two main scenarios:
- Photon Cloud Timeout: When a disconnect occurs due to a Photon Cloud Timeout, the callback will receive a
ShutdownReason.PhotonCloudTimeout
. - Session Closure: If the session was closed before a re-join attempt could succeed, the callback will receive a
ShutdownReason.GameNotFound
.
Behavior
When the Cloud Connection Lost feature is implemented:
- The peer will not shut down automatically upon losing connection to the Photon Cloud.
- Fusion will attempt to automatically re-join the session.
- Developers can implement custom logic to handle different scenarios based on the provided callback parameters.
Server Behavior
If the server loses connection to the Photon Cloud and is unable to re-join:
- The Fusion Plugin will automatically set the server as invisible and closed.
- No new clients will be able to join the session.
- Existing connected clients will remain connected and continue to play normally.
This ensures that the server continues to run and maintain the game session for already connected players, even when it loses connection to the Photon Cloud.
Implementation Example
Here's a basic implementation of the Cloud Connection Lost feature:
C#
public class NetworkRunnerCloudConnectionLost : MonoBehaviour
{
private void Start()
{
NetworkRunner.CloudConnectionLost += OnCloudConnectionLost;
}
private void OnCloudConnectionLost(NetworkRunner runner, ShutdownReason reason, bool reconnecting)
{
Debug.Log($"Cloud Connection Lost: {reason} (Reconnecting: {reconnecting})");
if (!reconnecting)
{
// Handle scenarios where reconnection is not possible
// e.g., notify the user, attempt manual reconnection, etc.
}
else
{
// Wait for automatic reconnection
StartCoroutine(WaitForReconnection(runner));
}
}
private IEnumerator WaitForReconnection(NetworkRunner runner)
{
yield return new WaitUntil(() => runner.IsInSession);
Debug.Log("Reconnected to the Cloud!");
}
}
This example demonstrates how to register the callback, handle different scenarios, and wait for a successful reconnection.
Best Practices
- Consider implementing a timeout for reconnection attempts to prevent indefinite waiting.
- Provide clear feedback to users about the connection status and any actions they need to take.
By leveraging this feature, developers can create more robust and resilient networked games that can handle temporary connection issues without disrupting the player experience.
Back to top