> 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/wang-luo/third-party-servers.md).

# 第三方服务器

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

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

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

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

也可以选择一种混合方案，通过 [serverless-multiplayer](/creator/content-creator-zh/chang-jing-sdk7/wang-luo/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 explorer 窗口。你必须在两个窗口中使用不同的地址连接。即使场景重新加载，两个会话也会保持打开。

作为替代方案，你可以在浏览器 URL 中输入以下内容，以打开第二个 Decentraland explorer 窗口：

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

## 独立领域

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

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

请参阅如何在 [获取玩家数据](/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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
