> 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 协议的实际运行，并使用开源的 [Comms Station](https://decentraland.github.io/comms-station/).
{% endhint %}

由于这些功能大多需要向所有附近玩家广播消息，因此它们会自动按接近程度分组为名为 *岛屿*的群组。每位玩家在同一时间只会被分配到一个岛屿，随着他们在世界中移动以及相对其他玩家的位置变化，这一分配也会改变。

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

{% @mermaid/diagram content="flowchart LR
subgraph Realm
Players\["Players<br/>⭐⭐⭐⭐⭐⭐⭐⭐⭐"]
Archipelago\["Archipelago"]

```
    subgraph Islands
        Island1["Island 1<br/>⭐⭐⭐⭐"]
        Island2["Island 2<br/>⭐⭐⚪⚪"]
        IslandN["Island n<br/>⭐⭐⭐⚪"]
    end
    
    Players --> Archipelago
    Archipelago --> Island1
    Archipelago --> Island2
    Archipelago --> IslandN
end" %}
```

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

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

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

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

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

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

{% @mermaid/diagram content="sequenceDiagram
participant Archipelago
participant Client
participant IslandBackend as Island Backend

```
Note over Client: Connect to Archipelago
Client->>Archipelago: Authenticate
Archipelago->>Client: Accept

Note over Client: Report position
Client->>Archipelago: Report position
Archipelago->>Client: Assign island

Note over Client: Connect to island
Client->>IslandBackend: Connect transport
Client->>IslandBackend: Authenticate
IslandBackend->>Client: Accept

Note over Client,IslandBackend: Exchange messages
Client->>IslandBackend: Player messages…
IslandBackend->>Client: Player messages…

Note over Client: Update position
Client->>Archipelago: Update position
Archipelago->>Client: Reassign island

Note over Client: Disconnect from old island
Client->>IslandBackend: Disconnect transport

Note over Archipelago,IslandBackend: Repeat cycle" %}
```

### 连接

comms 连接主要基于 websocket，不过某些传输方式可能会使用其他策略。

前往 [传输方式](https://github.com/decentraland/docs/blob/main/contributor/communications/transports/README.md) 或 [Archipelago](https://github.com/decentraland/docs/blob/main/contributor/communications/archipelago/README.md) 部分了解更多信息，或查看某个特定传输方式页面获取详细内容。

### 认证

对 comms 的连接会通过让客户端对服务器提供的挑战进行签名来完成认证，所使用的方案见 [认证链](https://github.com/decentraland/docs/blob/main/contributor/auth/authchain/README.md) 部分。

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

### 岛屿

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

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

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

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

获取岛屿分配并加入岛屿的流程详见 [Archipelago](https://github.com/decentraland/docs/blob/main/contributor/communications/archipelago/README.md) 部分。

### 消息

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

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

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