Find and join a session
Implement ways for joining multiplayer sessions by entering a join code, browsing available sessions, or reconnecting after a disconnection.
Read time 3 minutesLast updated a day ago
A player can join a previously created session in the following ways:
Join code
await MultiplayerService.Instance.JoinSessionByCodeAsync(joinCode);
A unique and human-friendly join code is generated when a host starts a session. A host can then share this join code with other players to invite them to play the same session. The codes use a combination of letters and numbers and are designed to be short and easy to type or spell out loud.
Refer to JoinSessionByCodeAsync.
Browse multiplayer sessions
You can generate a list of sessions in your game for users to browse and join. Users may want to filter this list based on different parameters to help them find a session that best matches what they're looking for.
You can use a session query to list sessions.
await MultiplayerService.Instance.QuerySessionsAsync(queryOptions);
Query results include all non-full, public sessions a player can join. Players can select a session from a list displaying only public properties of the session (such as its name), and join directly by session ID.
Refer to JoinSessionByIdAsync. Refer to QuerySessionOptions for available options.
Access rules
The session's access rules settings affect how players can join it. For more information, refer to Access rules.
Reconnect to a session
If a player is disconnected from a session but remains a member of it, they can reconnect to the same session. Implement the following flow in your game to handle the cases when a player disconnects.
Save the session type for reconnection
To reconnect to the session under the same , you must save the session's . If you set a when you created or joined the session, you must save the mapping between the session ID and the session type yourself, for example in or Cloud Save. The Multiplayer Services SDK doesn't save this information, and the service doesn't return it. Save the mapping as soon as the player creates or joins the session, so it's available if the game crashes.
TypeTypeTypePlayerPrefsFor more information about session types, refer to Session identifiers.
Reconnect
To reconnect a user, do the following:
-
Fetch all the sessions the current user is part of using GetJoinedSessionIdsAsync:await MultiplayerService.Instance.GetJoinedSessionIdsAsync();Use the mapping you saved to find the session type for each session ID.
-
To reconnect to the chosen session, call ReconnectAsync:await sessionManager.ReconnectAsync(sessionId);
You can add a custom network handler implementation to your reconnect request by specifying a that uses your own network handler implementation:
ReconnectSessionOptionsawait sessionManager.ReconnectAsync(sessionId,new ReconnectSessionOptions().WithNetworkHandler(customNetworkHandler));
Additional resources
- Network connection management for details on network connection options.
- QuerySessionsAsync for information about querying available sessions.
- QuerySessionsOptions for information about the query options for the search.
- QuerySessionsResults to return a list of available sessions from your query.
- JoinSessionByIdAsync for information on joining a session by the session ID.
- JoinSessionOptions for a list of available options.
- GetJoinedSessionIdsAsync to return a list of sessions the current user is a member of.
- ReconnectToSessionAsync to reconnect to a particular session.