ドキュメント

サポート

Multiplayer Services SDK

Multiplayer Services SDK

Session host migration

Learn about changing session ownership, including host election and data migration.
読み終わるまでの所要時間 2 分最終更新 6日前

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:
  • ISession.SessionHostChanged
    is raised when a new host is elected.
  • ISession.SessionMigrated
    is raised when data migration has completed.

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.
Host election doesn't migrate any network-synchronized data.

Data migration

The session system includes a default data migration implementation only for Netcode for Entities. If you use Netcode for GameObjects, use Distributed Authority to handle migration-related operations.
重要
Starting with Multiplayer Services SDK version 1.2, the default network handler implementation for Netcode for Entities automatically creates client and server worlds if none are available when starting a network connection through sessions.Older versions of the MPS SDK require you to create your Netcode for Entities client and server worlds before you can create or join a Multiplayer Services session. When you first add the Netcode for Entities package to your project, the default bootstrap automatically creates the client and server worlds at startup.For more advanced use cases, use a custom network handler to override the default integration with Netcode for Entities.
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
WithHostMigration
on the session options when creating or joining a session. Data migration requires an
IMigrationDataHandler
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
IMigrationDataHandler
to meet specific needs.
重要
Enabling the host data migration on a session doesn't set up the required
EnableHostMigration
entities component in your world. You must set up the required components manually, refer to Netcode for Entities for more information.
Use the
WithHostMigration
overloads to:
  • Configure the interval for automatic Netcode snapshot generation.
  • Set a timeout for snapshot uploads.
  • Provide a custom
    IMigrationDataHandler
    if
    EntitiesMigrationDataHandler
    doesn’t fit your use case.
  • Defaults: a 3-second snapshot interval and a 5-second upload timeout.
Hosts can also manage migration snapshots manually using
GetHostMigrationDataAsync
and
SetHostMigrationDataAsync
. These methods are primarily intended for custom
IMigrationDataHandler
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.

Using Relay

Added optional parameter
preserveRegion
to
RelayOptions
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
WithRelayNetwork
or
StartRelayNetwork
extension methods, depending on your use case.

Examples

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)}
Enabling the host data migration on a session doesn't set up the required
EnableHostMigration
entities component in your world. Set up the required components manually. Refer to Netcode for Entities for more information.

Example: Electing a new Host

The following code snippet demonstrates how to elect a given
player
by its ID as the new host:
public Task ElectPlayerAsHostAsync(string playerId){ _session.AsHost().Host = player.Id; return _session.AsHost().SavePropertiesAsync();}
After setting the
session
's host ID, save the changes to take effect.