ドキュメント

Multiplayer Services SDK

Multiplayer Services SDK

Matchmake to a local server

Use a Cloud Code allocator module to match players to a server running on your own machine during development.
読み終わるまでの所要時間 5 分最終更新 4日前

When you develop a multiplayer game, it's often faster to test matchmaking against a server that runs on your own machine than against a hosting provider. With Cloud Code hosting, you can do this with the LocalServerAllocator example module. The module sends matched players to a server that you already started, either on the same machine or on the same LAN (direct connection), or anywhere through Relay.
注
The
LocalServerAllocator
module is a workaround for current limitations of Cloud Code hosting. It's intended for development and testing only. Don't use it for production hosting.

Why a local server needs an allocator module

When
MultiplayerService.Instance.MatchmakeSessionAsync
receives an assignment, the client joins the session whose ID is the match ID, and doesn't retry if that session doesn't exist yet. A hosting provider starts a new server for every match, so that server knows the match ID from the start and creates its session under it.
A server on your machine is already running before any match exists. The server must receive the match ID so it can create the correct session, and the allocation must not be reported as ready until the server has created the session for that match. The
LocalServerAllocator
module does both.

How it works

  1. The server signs in with a service account and creates a control session with the ID
    local-<localServerId>
    , for example
    local-dev-a
    . It then watches that session's players.
  2. The client calls
    MatchmakeSessionAsync
    with the player property
    localServerId
    .
  3. Matchmaker forms a match from tickets with the same
    localServerId
    and calls the module's
    Matchmaker_AllocateServer
    function.
  4. The allocate function joins the control session as a dedicated allocator player and writes the match ID into that player's data. On later matches, it updates that data instead.
  5. The server detects the new match ID and creates the match session with
    MultiplayerServerService.Instance.CreateMatchSessionAsync(matchId, options)
    , on a direct network or through Relay.
  6. The module's
    Matchmaker_PollAllocation
    function returns
    pending
    until the match session exists and has published its network. It then returns an IP and port assignment for a direct network, or a custom assignment for Relay.
  7. The client joins the match session and connects to the server, directly or through Relay.

Set up matchmaking to a local server

The CONFIGURATION.md file of the module contains complete server and client scripts.
To set up matchmaking to a local server, follow these steps:
  1. Deploy the LocalServerAllocator module and its
    LocalServerQueue.mmq
    queue file, as described in the README.md file of the example repository. The queue file sets the pool's match hosting to the module:
    "matchHosting": { "type": "CloudCode", "moduleName": "LocalServerAllocator", "allocateFunctionName": "Matchmaker_AllocateServer", "pollFunctionName": "Matchmaker_PollAllocation"}
    The queue's
    SameServer
    rule matches only players with the same
    localServerId
    , so several developers can share a project and environment, each with their own server.
  2. Create a service account with permission to use Lobby, Relay, and Matchmaker in your project. Keep its credentials out of source control.
  3. Add the server script from CONFIGURATION.md to a scene with a
    NetworkManager
    . The server needs the Multiplayer Services package (
    com.unity.services.multiplayer
    ) 2.1.0 or later. Start it in Play Mode with a Server build profile active, or as a server build, and wait for its control session to be ready.
  4. Start a client in a separate process, such as a Player build or a Multiplayer Play Mode player, and matchmake with the same
    localServerId
    :
    var matchmakerOptions = new MatchmakerOptions{ QueueName = "local-server", PlayerProperties = new Dictionary<string, PlayerProperty> { ["localServerId"] = new PlayerProperty("dev-a") }};var session = await MultiplayerService.Instance.MatchmakeSessionAsync( matchmakerOptions, new SessionOptions { MaxPlayers = 1 });

Choose a direct connection or Relay

The server decides how clients connect when it creates the match session.
  • A direct network (
    WithDirectNetwork()
    ) suits clients on the same machine or LAN. There is no NAT traversal.
  • A Relay network (
    WithRelayNetwork()
    ) lets clients connect from anywhere. Relay connections count against your project's Relay usage.

Host from a player instead of a server

If you don't want to set up a service account or a server build, a regular player can stand in for the server. The player signs in anonymously, creates the control session, and hosts a session under each match ID. You can run it as a second Multiplayer Play Mode player next to your client. CONFIGURATION.md contains the host script.
The host is a player in the match session, so it takes a slot: set its maximum player count to the pool's maximum plus one. This variant doesn't create the session with the match's matchmaking results, so don't use it to test backfill functionality.

Limitations

Matchmaking on a local server has the following limitations:
  • A server hosts one match at a time. When the next match is allocated, the server ends the current match session and its players are disconnected. To host several matches at once, run several servers, each with its own
    localServerId
    .
  • If the server process is killed instead of shut down, its control session remains for a few minutes, and a new server with the same
    localServerId
    fails to start. Stop the server cleanly, wait for the old session to expire, or use a different
    localServerId
    .
  • The module reads the match session's network details from data that the Multiplayer Services SDK writes internally. If that format changes, the module needs to be updated.

Troubleshooting

Error

Cause and solution

No local server is running for '<id>'
No server is running for that
localServerId
in the queue's environment. Start the server and wait for its control session to be ready before you matchmake, and make sure the server, the client, and the deployed queue use the same environment.
A Session with the same identifier already exists
when the server starts
A previous server with the same
localServerId
didn't shut down cleanly. Refer to Limitations.
NetworkManagerStartFailed
on the client
The server is still running an earlier match. Keep the part of the server script that ends the previous match session before it creates the next one.

Additional resources