> 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 场景。

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

* **REST API + 数据库**：适用于不经常变化的数据（排行榜、留言簿）。玩家通过轮询 API 获取更新；状态会在会话之间持久保存。参见 [网络连接](/creator/content-creator-zh/chang-jing-sdk7/lian-wang/network-connections.md) 了解如何发起 `fetch` 从场景发出的请求。
* **WebSocket 服务器**：支持实时双向通信。参见 [网络连接](/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](/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，开启 **多实例预览** 在 **Desktop Client** 的子菜单 **播放选项**，然后再次点击 **播放** 按钮以打开第二个 Decentraland explorer 窗口。你必须在两个窗口中使用不同地址连接。随着场景重新加载，相同会话将保持打开。

或者，你可以通过在浏览器 URL 中输入以下内容来打开第二个 Decentraland explorer 窗口：

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

## 分离的领域

Decentraland 中的玩家存在于许多独立的 *领域*。不同领域中的玩家彼此无法看见，也无法互动或聊天，即使他们站在同一块地块上也是如此。这样划分玩家，使 Decentraland 能够在不遇到任何限制的情况下处理无限数量的玩家。它还会将彼此距离较近的玩家配对，以确保交互玩家之间的延迟时间可接受。

如果你的场景向第三方服务器发送数据，以便实时同步玩家之间的变化，那么重要的是，这些变化只能在同一领域内的玩家之间同步。你应该将属于一个领域的所有变化与其他领域的变化分开处理。否则，玩家会在没人做出更改的情况下，看到事物以一种诡异的方式发生变化。

了解如何在中获取每位玩家的领域，参见 [获取玩家数据](/creator/content-creator-zh/chang-jing-sdk7/jiao-hu-xing/user-data.md)

## 多人持久化

与玩家每次进入时都会重新挂载的本地场景不同，使用第三方服务器的场景其生命周期远远超出玩家进入和离开场景的时间。

因此，你必须在设计体验时考虑到玩家并不总会在相同的初始状态下看到场景。对场景所做的任何更改都会保留，供其他玩家发现；你必须确保这些更改不会以不希望的方式影响未来玩家的体验。

### 重置状态

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

在某些情况下，在场景中加入某种重置按钮是合理的。按下重置按钮会优雅地重置场景。

有时，这仅仅意味着将场景状态中的变量重置回默认值。但重置场景也可能涉及取消订阅监听器，以及停止服务器端的循环。如果每次重置场景时都残留空循环，这些循环就会不断堆积，并对场景性能产生不良影响。


---

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