> 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/contributor/contributor-zh/tong-xin/overview.md).

# 概述

Decentraland 通信系统，简称 *comms*，是一个实时消息协议，负责处理一个 realm 中玩家之间的交互。

其中一些交互由玩家发起，另一些则由客户端在幕后自动处理。示例如下：

* 文字和语音聊天
* 玩家移动时的位置更新
* 玩家更改外观时的头像更新

{% hint style="info" %}
你可以通过开源的 [Comms Station](https://decentraland.github.io/comms-station/).
{% endhint %}

由于这些功能大多需要向所有附近玩家广播消息，因此它们会自动按距离分组，称为 *岛屿*。每个玩家在同一时间只会被分配到一个岛屿；当他们在世界中移动并相对他人位置变化时，这一分配也会改变。

负责管理并将玩家分配到岛屿的 realm 服务称为 *Archipelago*。它负责在需要时创建岛屿，将其人数维持在合理范围内，并根据玩家移动动态重新分配玩家。

```mermaid
flowchart LR
    子图 Realm
        Players["玩家<br/>⭐⭐⭐⭐⭐⭐⭐⭐⭐"]
        Archipelago["Archipelago"]
        
        子图 Islands
            Island1["岛屿 1<br/>⭐⭐⭐⭐"]
            Island2["岛屿 2<br/>⭐⭐⚪⚪"]
            IslandN["岛屿 n<br/>⭐⭐⭐⚪"]
        end
        
        Players --> Archipelago
        Archipelago --> Island1
        Archipelago --> Island2
        Archipelago --> IslandN
    end
```

当客户端被分配到某个岛屿时，会获得一个该岛屿专属的 URI，用于连接实际的后端，由后端在它们之间转发消息。该连接会一直持续，直到 Archipelago 将客户端重新分配到另一个岛屿。

这意味着，除了 [Archipelago](https://github.com/decentraland/docs/tree/main/contributor/communications/archipelago/README.md) 协议之外，客户端还必须实现若干 *传输*，每个传输都以统一接口封装一个受支持的后端。

### 客户端生命周期 <a href="#lifecycle" id="lifecycle"></a>

comms 客户端的生命周期可概括为以下几个步骤：

1. **选择一个 realm**：获取该 [Archipelago](https://github.com/decentraland/docs/tree/main/contributor/communications/archipelago/README.md) 服务的 URI。
2. **加入 Archipelago**：与该服务建立持久连接。
3. **获取岛屿（重新）分配**：上报当前位置并获取一个岛屿专属 URI。
4. **连接一个传输**：向该岛屿专属后端建立第二条连接。
5. **重复**：继续执行第 3 和第 4 步，定期上报新位置。

当客户端结束会话时，只需断开与 Archipelago 服务的连接即可。它们将自动从当前岛屿中移除。

```mermaid
序列图
    参与者 Archipelago
    参与者 Client
    participant IslandBackend as 岛屿后端
    
    Note over Client: 连接到 Archipelago
    Client->>Archipelago: 认证
    Archipelago->>Client: 接受
    
    Note over Client: 上报位置
    Client->>Archipelago: 上报位置
    Archipelago->>Client: 分配岛屿
    
    Note over Client: 连接到岛屿
    Client->>IslandBackend: 连接传输
    Client->>IslandBackend: 认证
    IslandBackend->>Client: 接受
    
    Note over Client,IslandBackend: 交换消息
    Client->>IslandBackend: 玩家消息…
    IslandBackend->>Client: 玩家消息…
    
    Note over Client: 更新位置
    Client->>Archipelago: 更新位置
    Archipelago->>Client: 重新分配岛屿
    
    Note over Client: 断开旧岛屿连接
    Client->>IslandBackend: 断开传输
    
    Note over Archipelago,IslandBackend: 重复循环
```

### 连接

comms 连接主要基于 websocket，不过某些传输也可能采用其他策略。

前往 [传输](https://github.com/decentraland/docs/tree/main/contributor/communications/transports/README.md) 或 [Archipelago](https://github.com/decentraland/docs/tree/main/contributor/communications/archipelago/README.md) 章节以了解更多，或查看某个具体传输页面了解详情。

### 认证

comms 的连接通过让客户端对服务器提供的挑战进行签名来完成认证，使用的是 [身份验证链](https://github.com/decentraland/docs/tree/main/contributor/auth/authchain/README.md) 部分中所描述的相同规则。

前往 [Archipelago](https://github.com/decentraland/docs/tree/main/contributor/communications/archipelago/README.md) 页面，或查看某个特定传输，以了解更多认证流程。

### 岛屿

岛屿是高度动态的玩家群组，能够在组内相互广播消息，由 [Archipelago](https://github.com/decentraland/docs/tree/main/contributor/communications/archipelago/README.md) 根据他们在世界中的移动而创建和维护。

岛屿没有预定义的区域或中心点。它们不是稳定的地理实体，只是附近玩家之间的临时关联。如果说一个岛屿覆盖了某个区域，也只是因为其成员当前分布在那个区域。

在没有玩家的区域可以没有岛屿，而在人口密集的区域则可能存在多个重叠的岛屿；如果把所有人分到同一组，实时广播将变得不可能。

Decentraland 网络中的每台服务器都可以配置岛屿的最大人数，以及判定玩家彼此为附近时的距离。默认情况下，岛屿最多可容纳 100 名彼此相距 100 米以内的玩家。

获取岛屿分配并加入岛屿的流程详见 [Archipelago](https://github.com/decentraland/docs/tree/main/contributor/communications/archipelago/README.md) 部分中所描述的相同规则。

### 消息

comms 协议中的消息是二进制数据块，使用 [Protocol Buffers](https://github.com/protocolbuffers/protobuf)序列化，并封装在 [数据包](https://github.com/decentraland/docs/tree/main/contributor/communications/messages/README.md#Packet) 结构中。

{% hint style="info" %}
当使用 *消息* 一词在 comms 语境中时，我们始终指的是二进制消息协议，而不是玩家之间交换的聊天消息。
{% endhint %}

有多种不同的消息类型，用于各种实时交互流程。前往 [消息](https://github.com/decentraland/docs/tree/main/contributor/communications/messages/README.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/contributor/contributor-zh/tong-xin/overview.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.
