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.
Why a local server needs an allocator module
When 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.
MultiplayerService.Instance.MatchmakeSessionAsyncA 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 module does both.
LocalServerAllocatorHow it works
- The server signs in with a service account and creates a control session with the ID , for example
local-<localServerId>. It then watches that session's players.local-dev-a - The client calls with the player property
MatchmakeSessionAsync.localServerId - Matchmaker forms a match from tickets with the same and calls the module's
localServerIdfunction.Matchmaker_AllocateServer - 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.
- The server detects the new match ID and creates the match session with , on a direct network or through Relay.
MultiplayerServerService.Instance.CreateMatchSessionAsync(matchId, options) - The module's function returns
Matchmaker_PollAllocationuntil 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.pending - 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:
-
Deploy the LocalServerAllocator module and itsqueue file, as described in the README.md file of the example repository. The queue file sets the pool's match hosting to the module:
LocalServerQueue.mmq"matchHosting": { "type": "CloudCode", "moduleName": "LocalServerAllocator", "allocateFunctionName": "Matchmaker_AllocateServer", "pollFunctionName": "Matchmaker_PollAllocation"}The queue'srule matches only players with the sameSameServer, so several developers can share a project and environment, each with their own server.localServerId -
Create a service account with permission to use Lobby, Relay, and Matchmaker in your project. Keep its credentials out of source control.
-
Add the server script from CONFIGURATION.md to a scene with a. The server needs the Multiplayer Services package (
NetworkManager) 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.com.unity.services.multiplayer -
Start a client in a separate process, such as a Player build or a Multiplayer Play Mode player, and matchmake with the same:
localServerIdvar 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 () suits clients on the same machine or LAN. There is no NAT traversal.
WithDirectNetwork() - A Relay network () lets clients connect from anywhere. Relay connections count against your project's Relay usage.
WithRelayNetwork()
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 fails to start. Stop the server cleanly, wait for the old session to expire, or use a different
localServerId.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 server is running for that |
| A previous server with the same |
| 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. |