Reconnection

This module allows to deal with user disconnecting, then rejoining a session. It allows to identify that it is the same user, and to easily recover authority on their previously owned objects.
Logic overview
User id
A user will be identified by its user id Runner.GetPlayerUserId
.
If Fusion authentication is set, this value will be constant and based on the authentication logic set. But if it is not set, it will change at each connection: so, to handle this later case, the generated user id is stored in Unity's PlayerPrefs
.
The prefab spanwed for each player contains the ReconnectionHandler
component.
It stores the user identification information in the UserId
networked variable, and shared with other players.
A centralized ReconnectionManager
keeps tracks of all detected ReconnectionHandler
and store their user id (as well as a reference to their current ReconnectionHandler
).
Recoverable
Network objects can hold a Recoverable
network behaviour.
If the user ID was initially defined in a ReconnectionHandler
, when the same user reconnects, he will recovers state authority over this object.
Besides, onRecovery
event will be called for each recovered Recoverable
on the ReconnectionHandler
. The Recoverable
can also warn registered IRecoverableListener
.
ReconnectionManager / ReconnectionHandler registration
The communication between ReconnectionHandlers
and the ReconnectionManager
is based on the Registry/subscriber add-on.
Here, the ReconnectionManager
is a registry of ReconnectionHandler
(Registry<ReconnectionHandler>
), and the ReconnectionHandler
is the Subscriber
.
The availability of the ReconnectionHandler
is based on its UserId
being set, and the UserId
is set only when the ReconnectionManager
is detected as available (spawned), to be able to first check in its SessionUsers
if we are present as a reconnecting user, before publishing our own UserId
.
Reconnection Handling
In the event of unexpected disconnection, or even when the user quit the application normally on certain devices, the unregistration process may not be executed correctly.
So we need to manage the risk of duplicates user id when the player reconnects with the same user id.
To achieve this, the class lets you choose the desired behavior:
C#
public enum UserIdCollisionHandling {
// Consider the collision user id comes from a reconnecting user that is still in the room because its disonnection was not detected (under timeout and no clean quit)
AlwaysConsiderReconnectionUnderTimeout,
// Consider user id collision is just a collision, calling OnUserIdCollision (this handler won't take its Recoverable back)
AlwaysConsiderCollision,
// Check if LastReceivedTick evolves after collisionCheckLastReceivedTickEvolutionWindow seconds, to choose between reconnection (value does not evolve - probably handled by our previous disconnecting user) and collision (value is evolving). Mostly relevant in XR, where we are sure that the user always send data
CheckIfLastReceivedTickEvolves,
}
Setup
Scene
A ReconnectionManager
and its network object needs to be in the scene at start. It needs to allow state authority change (and not to be destroyed on state authority disconnection) on its network object, to ensure that it always has a state authority, no matter the disconnections.
It will then store an history of session users in the SessionUsers
network array.
The history is limited in the add-on to a MAX_USER_HISTORY
of 50 users, and this can be set to 100. Over this level, any new user could free the history of another disconnected user having reached a given timeout.
Note:Higher history count values can be used, but with a slightly different pattern, a bit more verbose - contact us if you need it.
Player object
The network object spawned for the player must have a ReconnectionHandler
.
Recoverable
To be recoverable, a Recoverable
needs to be added to a network object. It needs to allow state authority change (and not to be destroyed on state authority disconnection) on its network object, to ensure that the state authority could be recovered after a reconnection.
When spawning a recoverable object, make sure to set its Recoverable
's UserId
to the UserId
of the user's ReconnectionHandler
.
Dependencies
- XRShared addon
- Registry/Subscriber addon
Download
This addon latest version is included into the Industries addon project
Supported topologies
- shared mode
Changelog
- Version 2.0.0: First release