文档

Multiplayer Services SDK

Multiplayer Services SDK

Session states

Reference the lifecycle states a session can be in while it's created, connected, and removed.
阅读时间3 分钟最后更新于 1 天前

The
SessionState
enum represents the current lifecycle state of a multiplayer session. Use the session's
State
property or subscribe to
StateChanged
to react when a session transitions between states.
A session can be in one of the following states:

State

Description

When to use

None
The session has no active lifecycle state, either because it isn't initialized yet or because the player's authentication was cleared.Before a session is fully initialized, or after the player signs out.
Connected
The session is active and the client is connected to the session.During normal gameplay or while a session is available for players to participate in.
Disconnected
The client was connected to the session but has left it or been removed from it.After the player leaves or is removed, or while you handle reconnection.
Deleted
The session has been removed or deleted and is no longer valid for use.After the session has been closed or cleaned up.

What session state tracks

A session's state describes the local client's connection to the session. It doesn't describe the session's data or its player list:
  • A session keeps its data and properties while a client is
    Disconnected
    .
  • A player who loses their network connection without leaving is still a member of the session until the host or the service removes them. While they're still a member, they can reconnect.
  • To track other players joining and leaving, use session player events instead of the session state.

How a session moves between states

A session typically moves through the following lifecycle:
The following events change a session's state:

Transition

Cause

To
Connected
The client creates, joins, or reconnects to the session.
To
Disconnected
The client calls
LeaveAsync
, or the host removes the player. In both cases, the session also raises the
RemovedFromSession
event.
To
Deleted
The host or server calls
DeleteAsync
. This is the terminal state: you can't refresh or reconnect to a deleted session. To detect when another participant deletes the session, subscribe to the
Deleted
event.
To
None
The player's authentication access token is cleared, for example when the player signs out.

When to use this state information

Check the current state when your game logic depends on whether the session is usable, reconnecting, or already invalid. For example, you might defer gameplay setup until a session reaches
Connected
, show a "returned to menu" screen or a rejoin flow when a session becomes
Disconnected
, and stop all operations when the state becomes
Deleted
.
The following example listens for state changes and logs the current lifecycle state:
void OnSessionStateChanged(SessionState newState) { Debug.Log($"Session state changed: {newState}");}session.StateChanged += OnSessionStateChanged;

Additional resources