文档

​
​

Development

User Acquisition

Monetization

工业

Multiplayer Services SDK

All Services

Multiplayer Services SDK

此页面不支持所选语言。
Multiplayer
​
​
Multiplayer Services SDK
  • Overview
  • Get started
  • Use multiplayer sessions
  • Manage sessions
  • Connect players through a relay
  • Networking
    • Ports and protocols
    • Locations and regions
    • Quality of service
    • Scaling
    • DTLS encryption
    • Advanced configuration
      • Relay allocation service
      • Relay allocating, binding, and joining
      • Relay vs Lobby
      • Relay message protocol
      • Connection flow
      • Relay REST API
      • Manually configure Relay with Netcode for GameObjects
  • Matchmaking
  • Monitor and debug sessions
  • Tutorials
  • Reference
  1. Multiplayer Services SDK

Connection flow

Connect players through the Allocations service and Relay servers.
阅读时间5 分钟
最后更新于 2 个月前

The connection flow is the process by which the Allocations service reserves slots on a Relay server to group players into a match. The process involves two types of players: a host player and joining players. The following list describes the high-level steps involved in the connection process.
  1. The host player requests allocation
  2. The Allocations service selects a Relay server
  3. The Allocations service sends the connection data to the host player
  4. The host player binds to the Relay server
  5. The host player requests a join code
  6. The Allocations service returns a join code to the host player
  7. The host player shares the join code with joining players
  8. The joining players use the join code
  9. The Allocations service sends connection data to the joining player
  10. The joining player binds to the Relay server
  11. The joining player sends connection request

The host player requests an allocation

The host player initiates the connection flow by requesting an allocation from the Allocations service. The allocation request includes the maximum number of connections the allocation can allow. It can also include a specific region. If the request doesn’t include a region, the Allocations service uses QoS to select the best region for the request.

The Allocations service selects a Relay server

The Allocations service receives the host player's request and selects an appropriate Relay server. The Relay server selection depends on the maximum connections allowed and the region.
When the Allocations service finds a Relay server, it reserves a space on the Relay server for the session. At this point, the Relay server also generates a unique secret key. The Relay server returns the secret key to the Allocations service, in addition to the Relay server IP, the Relay server ports, and the connection data.

The Allocations service sends the connection data to the host player

The Allocations service sends the Relay server connection data to the host player's game client.

The host player binds to the Relay server

The host sends a
BIND
message
to the selected Relay server using the data received from the response from the allocation request. The
BIND
message has the connection data, the accept mode, nonce, and HMAC. If the host doesn’t send the bind to the Relay server within 60 seconds after making the allocation, the allocation times out from inactivity.
If the information in the request is accurate, the Relay server acknowledges the bind request by sending a
BIND_RECEIVED
message
back to the host client.
注意
You typically send
BIND
messages after calling the allocate and join APIs, which retrieve the Relay server’s destination address for use with the
BIND
message.

The host player requests a join code

Once bound to the Relay server, the host player can request a join code from the Allocations service.

The Allocations service returns a join code to the host player

The Allocations service generates and returns a join code to the host player. The join code the Allocations service returns uniquely represents the host player’s allocation to the Relay server, and allows joining players to bind to the same Relay server and connect to the host player.

The host player shares the join code with joining players

The host player shares the unique join code with other players through any method, including verbally, through a text message, or through a Lobby. The join codes are short and easy to remember to ease sharing.
The players that use the join code with a join request to the Allocations service become the joining players.
注意
Any number of joining players can use the same join code so long as the number doesn't exceed the maximum number of connections specified in the initial allocation request.

The joining players use the join code

The joining players use the join code from the host player to send a join request to the Allocations service.
注意
A “join” is when a joining player (a non-host player) client joins the host client’s session. Under the hood, it’s another allocate call to the Relay backend service.

The Allocations service sends connection data to the joining player

The Allocations service uses the join code to look up the host player’s allocation and returns the data to the joining players.
The response from the Allocations service has the Relay server IP address, the Relay server port, the secret key, the encrypted joining player’s connection data, the joining player’s allocation ID, and the encrypted host connection data. The joining player can then use the secret key to decrypt and use the host connection data to connect to the host.

The joining player binds to the Relay server

The joining player sends a
BIND
message
to the Relay server using its connection data it received from the response to the join request made to the Allocations service.
Upon success, the Relay server acknowledges by sending a
BIND_RECEIVED
message
to the joining player's game client.
注意
You should typically send
BIND
messages after calling the allocate and join APIs, which retrieve the Relay server’s destination address for use with the
BIND
message.

The joining player sends a connection request

Once bound to the same Relay server as the host player, the joining player then sends a connection request to the host player. If the connection request is successful, the joining player and the host player can send data to each other through the Relay server.

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • The host player requests an allocation

    • The Allocations service selects a Relay server

    • The Allocations service sends the connection data to the host player

    • The host player binds to the Relay server

    • The host player requests a join code

    • The Allocations service returns a join code to the host player

    • The host player shares the join code with joining players

    • The joining players use the join code

    • The Allocations service sends connection data to the joining player

    • The joining player binds to the Relay server

    • The joining player sends a connection request


报告此页面的问题