> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/lian-wang/third-party-servers.md).

# 第三方服务器

使用第三方服务器构建多人 Decentraland 场景。

场景中多人交互的推荐方式是使用 [多人服务器](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/lian-wang/authoritative-servers.md)。本文档介绍其他同样受支持的选项。

* **REST API + DB**：适用于不常变化的数据（排行榜、留言簿）。玩家会轮询 API 以获取更新；状态会在会话之间持久保存。请参阅 [网络连接](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/lian-wang/network-connections.md) ，了解如何在场景中发起 `fetch` 请求。
* **WebSocket 服务器**：支持实时双向通信。请参阅 [网络连接](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/lian-wang/network-connections.md) 了解 WebSocket 的用法。 [Colyseus](https://colyseus.io/) 等库与 Decentraland SDK 配合良好。

{% hint style="warning" %}
**📔 注意**：第三方服务器无法与 `syncEntity`, `validateBeforeChange`或 `Storage` 集成——你需要自行实现状态管理和同步逻辑。
{% endhint %}

你也可以采用混合方案，通过 [serverless-multiplayer](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/lian-wang/serverless-multiplayer.md)在玩家之间通知变更，同时通过 REST API 存储最终状态，供后续访客查看。

以下是一些使用第三方服务器的场景示例：

* [排行榜](https://github.com/decentraland/sdk7-goerli-plaza/tree/main/leader-board)
* [留言簿](https://github.com/decentraland/sdk7-goerli-plaza/tree/main/guest-book-api)
* [验证真实性](https://github.com/decentraland/sdk7-goerli-plaza/tree/main/validate-player-authenticity)

## 本地测试

要预览使用第三方服务器的场景，必须同时运行场景及其依赖的服务器。为了便于测试，可以在与预览相同的机器上本地运行服务器。本地运行时，服务器可以使用不安全的 `http` 或 `ws` 连接，以便简化设置。

要启动服务器，请进入 `/server` 文件夹并运行 `npm run start`.

在两个单独的窗口中打开预览，每个窗口都视为一名独立玩家。使用不同地址连接每个窗口。两个客户端都会连接到同一个本地服务器实例。

使用 Creator Hub，在 **多实例预览** 的 **桌面客户端** 子菜单中打开该选项，然后点击 **播放选项**中的 **播放** 按钮，再打开一个 Decentraland 浏览器窗口。你必须使用不同的地址分别登录两个窗口。重新加载场景时，这些会话仍会保持打开状态。

或者，你也可以在浏览器地址栏中输入以下内容，打开第二个 Decentraland 浏览器窗口：

> `decentraland://realm=http://127.0.0.1:8000&local-scene=true&debug=true&multi-instance=true`

## 不同的 Realm

Decentraland 中的玩家分布在许多不同的 *Realm*中。不同 Realm 的玩家看不到彼此，也无法互动或聊天，即使他们身处同一地块也一样。这样划分玩家可以让 Decentraland 容纳无限数量的玩家，而不受任何限制。此外，系统会将处于相邻区域的玩家配对，确保互动玩家之间的 ping 延迟处于可接受范围。

如果你的场景会将数据发送到第三方服务器，以便实时同步玩家之间的变更，那么务必只在同一 Realm 的玩家之间同步变更。你应将属于一个 Realm 的所有变更与其他 Realm 的变更分开处理。否则，玩家会看到场景中的事物诡异地发生变化，却没人做出过这些更改。

请参阅 [获取玩家数据](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/user-data.md)

## ，了解如何获取每位玩家所在的 Realm。

多人游戏状态持久化

与玩家每次进入都会重新加载的本地场景不同，使用第三方服务器的场景，其生命周期会远远超出玩家进入和离开场景的时间。因此，设计体验时必须考虑到玩家并不总会看到场景处于相同的初始状态。对场景所做的任何更改都会保留下来，供其他玩家看到；你必须确保这些更改不会以不理想的方式影响后续玩家的体验。

### 重置状态

加载场景时，请确保场景基于服务器中存储的共享信息构建，而不是处于默认状态。

在某些情况下，在场景中添加某种重置按钮是合理的。按下重置按钮可以平稳地重置场景。

有时，这只意味着将场景状态中的变量恢复为默认值。但重置场景也可能需要取消订阅监听器，并停止服务器端的循环。如果每次重置场景后都留下空循环，它们就会不断累积，进而影响场景的性能。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/lian-wang/third-party-servers.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
