Skip to main content

Reconnecting Mid-Game

In a multiplayer game, clients may get disconnected from the server for a variety of reasons (network issues, application/device crashes, etc). For those reasons, you may want to allow your players to reconnect to the game.

Considerations

Review the following considerations before allowing clients to reconnect in the middle of an active game:

  • Clients may have specific state and in-game data associated with them. If you want them to reconnect with that same state and data, implement a mechanism to keep the association of state and data with the client for when they reconnect. See Session Management for more information.

  • Depending on your game's requirements, make sure the client's state is properly reset and ready to connect before attempting reconnection. This may require resetting some external services.

  • When using scene management and multiple additive scenes, there is a specific case to keep in mind. During the synchronization process, which launches when connecting to a game, if the client's active main scene is the same as the server's, it won't initiate a scene load in single mode for that scene. Instead, it will load all additive scenes that are currently loaded on the server. This means that if the client has additive scenes currently loaded, it won't unload them like it would if the client's main scene was different than the server's.

    • For example, if during a game, the server loads main scene A, then additively loads scenes B and C, the client has all three loaded. If the client disconnects and reconnects without changing scenes, the scene synchronization process recognizes that main scene A is already loaded on the client, and then simply proceeds to load the server's additive scenes. In that case, the client loads the scenes B and C a second time and then has two copies of those scenes loaded.

    However, if, while the client is disconnected, the server loads or unloads a scene additively, there is also a mismatch between the scenes loaded on the client and on the server. For example, if the server unloads scene C and loads scene D, the client loads scene D when synchronizing, but doesn't unload scene C, so the client has loaded scenes A, B (twice), C, and D, while the server only has loaded scenes A, B, and D.

    • If you want to avoid this behavior, you can
      • Load a different scene in single mode on the client when disconnecting
      • Unload additive scenes on the client when disconnecting
      • Unload additive scenes on the client when reconnecting
      • Use the NetworkSceneManager.VerifySceneBeforeLoading callback to prevent loading scenes already loaded on the client. However, this won't handle unloading scenes that were unloaded by the server between the time the client disconnected and reconnected.

Automatic reconnection

For a smoother experience for your players, clients can automatically attempt to reconnect to a game when connection is lost unexpectedly.

To implement automatic reconnection:

  • Define what will trigger the reconnection process and when it will start.
    • For example, you can use the NetworkManager.OnClientDisconnectCallback callback, or some other unique event depending on your game.
  • Ensure the NetworkManager properly shuts down before attempting any reconnection.
    • You can use the NetworkManager.ShutdownInProgress property to manage this.
  • Add additional checks to ensure automatic reconnection does not trigger under the wrong conditions.
    • Examples
      • A client purposefully quits a game
      • The server shuts down as expected when the game session ends
      • A client gets kicked from a game
      • A client is denied during connection approval

Depending on your game, you may want to add the following features as well:

  • Include multiple reconnection attempts in case of failure. You need to define how many attempts these should be, ensure that NetworkManager properly shuts down between each attempt, and reset the client's state (if needed).
  • Provide an option for players to cancel the reconnection process. This may be useful when there are a lot of reconnection attempts or when each attempt lasts a long duration.

Automatic reconnection example

Check out our Boss Room sample for an example implementation of automatic reconnection.

The entry point for this feature is in this class. Boss Room's implementation uses a state inside a state machine that starts a coroutine on entering it (ReconnectCoroutine) that attempts to reconnect a few times sequentially, until it either succeeds, surpasses the defined maximum number of attempts, or is cancelled. (See OnClientConnected, OnClientDisconnect, OnDisconnectReasonReceived, and OnUserRequestedShutdown)

The reconnecting state is entered when a client disconnects unexpectedly. (See OnClientDisconnect in this class)

note

This sample connects with Lobby and Relay services, so the client must make sure it has properly left the lobby before each reconnection attempt.

For more information about how Boss Room leverages Lobby and Relay, see Getting Started with Boss Room