> ## 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.

# 使用 LocalMultiplayerAgent 调试基于进程的游戏服务器

> 通过包装现有服务器并以进程(非容器)模式在 LocalMultiplayerAgent 中运行,在本地调试 PlayFab 多人游戏服务器。

# 如何在进程模式下使用 LocalMultiplayerAgent 运行游戏服务器

本教程介绍如何:

* 使用 Wrapper 示例创建生成
* 配置 MultiplayerSettings.json
* 运行 LocalMultiplayerAgent
* 测试游戏连接

## 创建游戏生成

若要使用 LocalMultiplayerAgent(LMA)测试游戏服务器,你需要创建游戏生成并保存为存档格式。
为了方便你,我们提供了 Wrapper 示例以及说明和 PowerShell 脚本。

接下来,按照"[使用 wrapper 应用包装现有游戏服务器](https://github.com/PlayFab/MpsSamples/tree/main/wrappingGsdk#wrapping-an-existing-game-server-using-the-wrapper-app)"步骤准备好本教程使用的游戏服务器。

## 配置 MultiplayerSettings.json

导航到解压 LMA 工具集的文件夹并打开 MultiplayerSettings.json 文件。此文件是一个生成配置模拟文件,用于模拟 MPS 上的生成。

你可以使用 [LMA MultiplayerSettings.json Generator](https://github.com/PlayFab/MpsAgent/tree/main/LocalMultiplayerAgent/SettingsJsonGenerator) 填充 json。
Generator 是一个简单的网页,它根据你的选项创建 json。你可以在 LMA 的解压文件夹下找到 Generator。

下面是在进程模式下运行 LMA 时 Wrapper 示例的 MultiplayerSettings.json 示例。

```json theme={null}
{
  "RunContainer": false, // Set RunContainer to false if you are running LMA in Process mode.
  "OutputFolder": "C:\\output\\WindowProcess", // Path where config files and logs will be generated from LMA at each run
  "NumHeartBeatsForActivateResponse": 10, // default value
  "NumHeartBeatsForTerminateResponse": 60, // default value
  //the server will run in stand-by mode for 10 heartbeats and stay in active state for 35 heartbeats.
  "TitleId": "59F84", // default value
  "BuildId": "8624d52e-87a1-4a12-b7f6-b92720ef8919", // default value
  "Region": "EastUs", // default value
  "AgentListeningPort": 56001, // default value
  "AssetDetails": [
    {
      "MountPath": "",  // Mount Path is only required for Container mode. leave it as blank.
      "SasTokens": null,
      "LocalFilePath": "D:\\gameassets.zip" // where your game server is located as an archive format.
    }
  ],
  "ProcessStartParameters": {
    "StartGameCommand": "wrapper.exe -g fakegame.exe arg1 arg2" 
    // shell command to run Wrapper. Command will be executed where game asset is extracted.
    // Make sure the StartGameCommand provided above is an example of the Wrapper sample. 
  },
  "PortMappingsList": [
    [
      {
        "NodePort": 56100, // default value
        "GamePort": {
          "Name": "game_port",
           // Port name is already defined in the Wrapper sample. Wrapper will grab the port information using game_port via GSDK while it's running.
          "Protocol": "TCP"
        }
      }
    ]
  ],
}

```

请确保正确更新 MultiplayerSettings.json 中的以下字段以在进程模式下运行 LMA。

* `LocalFilePath` — 之前创建的游戏服务器资产 zip 文件的完整本地路径(在你的工作站上),例如:D:\MyAmazingGame\asset.zip(注意反斜杠需要为 JSON 格式进行转义)。

* `StartGameCommand` — 进程的 StartGameCommand 路径是一个相对路径。工作目录将是游戏资产解压的位置。

* `PortMappingsList` — 这些是游戏运行时可用的端口。
  * `NodePort` 是在你的工作站上打开的端口

  * `GamePort.Number` 在进程模式下不需要。当游戏服务器运行时,端口号将通过 GSDK 绑定到 Node Port。在实际场景中,MPS 会为每个基于进程的游戏会话动态绑定端口。

  * 将 `GamePort.Name` 设为与游戏服务器中定义的相同值。你可以在运行时通过键 GamePort.Name 检查 GSDK 配置来获取该值。如果你使用 Wrapper 示例,端口名称已定义为 "game\_port",因此此处应设为相同的值。

  * `GamePort.Protocol` — 指定协议类型:TCP 或 UDP

* `OutputFolder` — 用于生成输出和配置文件的驱动器或文件夹路径。请确保有足够的可用空间,因为游戏服务器将在此路径下解压。如果未指定,则使用代理文件夹。

* `AgentListeningPort` — 这是 LMA 与游戏服务器通信的端口。任何开放的端口都可以,56001 是默认值。如果有其他进程绑定到 56001,你必须更改此值或终止占用 56001 端口的其他进程。

* `ResourceLimits` — 进程模式下不需要。

* `MountPath` — 进程模式下不需要。

* `SessionCookie`(可选)— 作为 RequestMultiplayerServer API 调用一部分传递给游戏服务器的任何会话 Cookie。在 MPS 的实际场景中,建立连接后,服务器将通知客户端从 SessionCookie 加载对应的资源。

## 运行 LocalMultiplayerAgent

现在你可以准备运行 LocalMultiplayerAgent。

* 在 PowerShell 窗口中:\
  导航到 LMA 下包含 LocalMultiplayerAgent.exe 的目录。

* 运行 `LocalMultiplayerAgent.exe`。
  此时,LMA 设置 http 侦听器,解压游戏资产,并在单独的进程中启动游戏服务器。

  > 如果你以进程方式运行游戏服务器,Windows 防火墙可能会弹出,询问你是否允许流量通过所指定的 NodePort。如果想避免这种情况,你可以以管理员模式运行 LocalMultiplayerAgent,或者在防火墙中启用该端口。

LMA 将等待来自与游戏服务器集成的 GSDK 的心跳。
如果 GSDK 集成正确,LMA 将按以下顺序打印输出:

1. `CurrentGameState - Initializing`\
   (如果你的游戏服务器直接调用 GSDK::ReadyForPlayers 而不调用 GSDK::Start,则可能不会显示)
2. `CurrentGameState - StandingBy`
3. `CurrentGameState - Active`
4. `CurrentGameState - Terminating`

若要详细了解游戏服务器的状态,请参阅 [PlayFab Multiplayer Server 的游戏服务器生命周期是什么](/services/playfab/multiplayer/servers/multiplayer-game-server-lifecycle)。

如果关闭回调设置正确,则在状态设为 terminating 后不久,你的游戏服务器将退出。
验证游戏服务器是否退出很重要,以避免在 PlayFab 平台上出现不正常的关闭。

LMA 也应随游戏一起终止。

## 测试与游戏的连接

一旦 LMA 打印 **CurrentGameState - Active**,你就可以使用 IP 地址 127.0.0.1 和你的游戏服务器监听的端口 NodePort 连接到游戏服务器。

如果你使用 Wrapper 示例,可以通过在浏览器中输入地址 [http://127.0.0.1:56100/Hello](http://127.0.0.1:56100/Hello) 来测试 GET 请求。
有关详细信息,请查看 Wrapper 示例。

你还可以更新 MultiplayerSettings.json 中的 **NumHeartBeatsForActivateResponse** 和 **NumHeartBeatsForTerminateResponse** 的值,以调整 standing-by/active 状态的持续时间。


## Related topics

- [使用 LocalMultiplayerAgent 调试容器游戏服务器](/zh-CN/services/playfab/multiplayer/servers/LocalMultiplayerAgent/run-container-gameserver.md)
- [LocalMultiplayerAgent 概述](/zh-CN/services/playfab/multiplayer/servers/LocalMultiplayerAgent/local-multiplayer-agent-overview.md)
- [在本地调试游戏服务器以及与 PlayFab 的集成](/zh-CN/services/playfab/multiplayer/servers/locally-debugging-game-servers-and-integration-with-playfab.md)
- [GSDK 项目测试和调试](/zh-CN/services/playfab/multiplayer/servers/server-sdks/unreal-gsdk/third-person-mp-example-project-local-deployment-and-debugging.md)
- [创作游戏服务器生成](/zh-CN/services/playfab/multiplayer/servers/author-a-game-server-build.md)
