> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 与 PlayFab Multiplayer Servers 集成

> 了解如何配置 PlayFab 匹配队列,以便为每个完成的对局自动分配 Multiplayer Server build 或 build 别名。

匹配包含一个选项,可为最终对局自动创建游戏服务器。分配的服务器运行一个 build,该 build 在队列配置中进行配置。启动时,服务器会以对局中的成员作为初始玩家列表传入。

匹配队列与单个多人游戏服务器 build 或 build 别名相关联。

## 使用匹配与服务器直接集成的要求

为了从匹配中分配多人游戏服务器,你首先需要配置一个 build 并部署它。你也可以选择创建 build 别名并使用它。build 别名更受推荐,因为它们为游戏使用提供更多灵活性。请通过我们此处提供的链接了解有关 [Multiplayer Server build](/services/playfab/multiplayer/servers/deploying-playfab-multiplayer-server-builds) 和 [Multiplayer Server build 别名](/services/playfab/multiplayer/servers/allocating-with-build-alias)的更多信息。

匹配还需要在队列中添加区域选择规则,以便可以在 build 的最佳区域分配对局。传入区域选择规则的延迟测量应与该 build 处于活动状态的区域相匹配。有关如何将属性传递给区域选择规则的更多信息,请参阅[区域选择规则](/services/playfab/multiplayer/matchmaking/ticket-attributes#region-selection-rule)。

## 为队列激活服务器分配

要为队列启用服务器分配,你需要在队列配置页面上勾选 **Enable server allocation** 复选框。启用后,选择 **Build alias** 或 **Build Id**。从下拉列表中选择队列要使用的正确值。启用此功能后,匹配将尝试为该队列中创建的所有对局分配服务器。

以下示例是使用 Build Id 时具有多人游戏服务器集成的队列配置。

```json theme={null}
"MatchmakingQueue": {
    "Name": "ServerEnabledQueue",
    "MinMatchSize": 2,
    "MaxMatchSize": 2,
    "ServerAllocationEnabled": true,
    "BuildId": "88b3e315-829c-4b6d-9872-74f427ad5331",
	"RegionSelectionRule": {
		"MaxLatency": 1000,
		"Path": "Latencies",
		"Weight": 1.0,
		"Name": "RegionSelectionRule"
	}
}
```

下面是使用 build 别名的相同示例。

```json theme={null}
"MatchmakingQueue": {
    "Name": "ServerEnabledQueue",
    "MinMatchSize": 2,
    "MaxMatchSize": 2,
    "ServerAllocationEnabled": true,
    "BuildAliasParams": {
        "AliasId": "d14e2ac7-ea51-47aa-a7ba-cc427ab74f9s"
    },
	"RegionSelectionRule": {
		"MaxLatency": 1000,
		"Path": "Latencies",
		"Weight": 1.0,
		"Name": "RegionSelectionRule"
	}
}
```

启用服务器分配后,匹配的调用流程保持不变。匹配为对局分配服务器后,可通过调用 [GetMatch](xref:titleid.playfabapi.com.multiplayer.matchmaking.getmatch) 从 Match 对象本身读取生成的服务器详细信息。

以下是启用了服务器分配的队列的 `GetMatch` 调用的示例响应。

```json theme={null}
{
    "MatchId": "7c36330d-46b5-443f-8d8f-10390bce09d5",
    "Members": [{
        "TeamId": "",
        "Entity": {
            "Id": "67282A13A1A58910",
            "Type": "title_player_account",
            "TypeString": "title_player_account"
        },
        "Attributes": {
            "DataObject": null,
            "EscapedDataObject": "{\"StringEqualityRulePath\":\"0bc42969-76b1-4dcb-871d-d6e19cee741b\"}"
        }
    },
    {
        "TeamId": "",
        "Entity": {
            "Id": "6DB2B17FD21CC230",
            "Type": "title_player_account",
            "TypeString": "title_player_account"
        },
        "Attributes": {
            "DataObject": null,
            "EscapedDataObject": "{\"StringEqualityRulePath\":\"0bc42969-76b1-4dcb-871d-d6e19cee741b\"}"
        }
    }],
    "RegionPreferences": ["WestUs", " EastUs"],
    "ServerDetails": {
        "IPV4Address": "40.76.31.170",
        "Ports": [{
            "Name": "game_port",
            "Num": 30003
        }]
    }
}
```

客户端可以使用响应中服务器详细信息部分的 IP 和端口连接到服务器。有关如何将客户端连接到多人游戏服务器的更多信息,请参阅[将客户端连接到服务器](/services/playfab/multiplayer/servers/connecting-clients-to-game-servers)。

<Note>
  当你的 build 的某个区域容量耗尽时,匹配将无法在该区域中为队列分配对局。匹配将持续重试分配,直到工单过期。要增加多人容量,请参阅[获取增加的核心数上限和其他 Azure 区域](/services/playfab/multiplayer/servers/identifying-and-increasing-core-limits)。
</Note>

## 传递给游戏服务器的信息

以下是匹配传递给服务器的信息列表。

* **SessionId** - 服务器的 `SessionId` 等于对局的 MatchId。
* **InitialPlayers** - 此值设置为对局中成员的列表。可以在游戏中通过使用 [GSDK](/services/playfab/multiplayer/servers/integrating-game-servers-with-gsdk) 读取玩家列表。
* **PreferredRegions** - 此字段设置为对局的 `RegionPreferences` 字段。游戏服务器服务从此列表中为服务器选择合适的区域。

匹配不会向游戏服务器传递任何工单属性。如果游戏需要在服务器上访问任何工单属性,可以通过将 `ReturnMemberAttributes` 头设置为 `true` 调用 [GetMatch](xref:titleid.playfabapi.com.multiplayer.matchmaking.getmatch) 来实现。

<Note>
  我们最近启用了在服务器分配期间传递队列名称。此功能处于预览阶段,你可在[此处](https://github.com/PlayFab/MpsSamples/blob/main/MatchmakeSample/matchMakingAndMps.md)阅读更多信息。
</Note>


## Related topics

- [匹配](/zh-CN/services/playfab/multiplayer/matchmaking/index.md)
- [Matchmaking 快速入门](/zh-CN/services/playfab/multiplayer/matchmaking/quickstart.md)
- [与 PlayFab Lobby 和 Match 集成](/zh-CN/services/xbox-services/multiplayer/mpa/concepts/live-mpa-playfab-integration.md)
- [使用多人 SDK 将 Party 与 Lobby 集成](/zh-CN/services/playfab/multiplayer/networking/party-lobby-integration.md)
- [在本地调试游戏服务器以及与 PlayFab 的集成](/zh-CN/services/playfab/multiplayer/servers/locally-debugging-game-servers-and-integration-with-playfab.md)
