Migrate session host
Learn about changing session ownership, including host election and data migration.
読み終わるまでの所要時間 2 分最終更新 3ヶ月前
In the sessions system, migration involves two aspects: host election, and data migration.
The session system exposes to notify you about changes in session ownership and migration status:
- is raised when a new host is elected.
ISession.SessionHostChanged - is raised when data migration has completed.
ISession.SessionMigrated
Host election
Host election allows changing the session’s host. The current host can start this by updating the session’s assigned host ID. For the change to take effect, save all session properties so the update propagates to all participants.
Data migration
Use host data migration to keep a client-hosted session running after the host is lost. This supports both voluntary and involuntary interruptions, such as network disconnections, power failures, or the host quitting the application.
Enable host data migration for a session by calling on the session options when creating or joining a session. Data migration requires an implementation, which defines how network-synchronized data is captured and applied. A default handler is provided for Netcode for Entities (minimum version 1.7.0). You can also supply a custom to meet specific needs.
WithHostMigrationIMigrationDataHandlerIMigrationDataHandlerUse the overloads to:
WithHostMigration- Configure the interval for automatic Netcode snapshot generation.
- Set a timeout for snapshot uploads.
- Provide a custom if
IMigrationDataHandlerdoesn’t fit your use case.EntitiesMigrationDataHandler - Defaults: a 3-second snapshot interval and a 5-second upload timeout.
Hosts can also manage migration snapshots manually using and . These methods are primarily intended for custom implementations. Internally, they use the Lobby service to store and retrieve network snapshots and are automatically invoked by the NetworkModule when a data migration is detected.
GetHostMigrationDataAsyncSetHostMigrationDataAsyncIMigrationDataHandlerUsing Relay
Added optional parameter to to configure relay reallocation behavior during host migration. Setting this parameter to true saves the region of the first relay allocation and reuses it when a relay server is reallocated during host migration. Configure RelayOptions via the or extension methods, depending on your use case.
preserveRegionRelayOptionsWithRelayNetworkStartRelayNetworkExamples
Example: Creating a custom IMigrationData
Implement a data migration handler.
public class CustomDataHandler : IMigrationDataHandler{ public byte[] Generate() { // implement a process to serialize data based on the network component in the scene / world return new byte[42]; } public void Apply(byte[] migrationData) { // implement a process to de-serialize data and apply in current scene / server world Assert.AreEqual(42, migrationData.Length); }}
Use the custom implementation with the sessions.
public Task CreateOrJoinSessionWithDataMigartionEnabled(string sessionId){ var sessionOptions = new SessionOptions().WithHostMigration(migrationDataHandler: new CustomDataHandler()); var session = await MultiplayerService.Instance.CreateOrJoinSessionAsync(sessionId, options)}
Example: Electing a new Host
The following code snippet demonstrates how to elect a given by its ID as the new host:
playerpublic Task ElectPlayerAsHostAsync(string playerId){ _session.AsHost().Host = player.Id; return _session.AsHost().SavePropertiesAsync();}
After setting the 's host ID, save the changes to take effect.
session