> 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/transports.md).

# 传输方式

一个 *传输* 是一个客户端侧接口，可连接到 comms URI，并与对等方或服务交换实时消息。

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

客户端可自行实现传输，只要它们遵循其他客户端和服务所期望的协议即可，但下面的 [推荐设计](#recommended) 在实践中已经过测试，并被证明能与不同项目很好地集成。\`

### 标准传输 <a href="#types" id="types"></a>

协议定义了 5 种传输类型：

* [WebSocket](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/websocket/README.md) (`ws-room`) 使用标准的 WebSocket 流。
* [LiveKit](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/livekit/README.md) (`livekit`) 使用基于 WebRTC 的开源 LiveKit 框架。
* [SignedLogin](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/signed_login/README.md) (`signed-login`) 发起带签名的 HTTPS 请求，以从服务器获取动态分配的传输。
* [离线](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/offline/README.md) (`offline`) 是一个占位传输，表示当前环境中没有通信。
* [模拟器](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/simulator/README.md) (`simulator`) 是一个带有完全自定义行为的占位传输，主要供开发者调试其实现。

通过传输接口发送和接收的消息始终是相同的（参见 [消息](https://github.com/decentraland/docs/tree/main/contributor/communications/messages/README.md)）。某些传输可能会在传输过程中将它们封装在控制结构中，但在跨越 `传输` 接口。

### 推荐设计 <a href="#recommended" id="recommended"></a>

这是一个最小的 `传输` 用 TypeScript 编写的接口：

```ts
interface Transport {
  // 使用 URI 初始化传输：
  constructor(private uriWithConnectionParams: string)

  // 使用构造函数中给定的 URI 打开连接：
  connect(): Promise<void>

  // 关闭连接：
  disconnect(): Promise<void>

  // 向服务和所有对等方发送任意载荷：
  send(packet: Packet): Promise<void>

  // 订阅来自服务和所有对等方的传入消息：
  on(event: 'receive', callback: (packet: Packet) => void): void
}
```

实际实现通常会扩展它，增加连接/断开和加入/离开事件、非广播发送、更严格的类型检查或特定语言的适配。

#### 传输 URI

……的值 `uriWithConnectionParams` 始终采用以下形式：

```
<类型>:<类型连接参数>
```

该 `<类型>` 对应于上面列出的某一种，而 `<类型连接参数>` 取决于具体的传输。它可以是一个 URL、一个不透明令牌或任意数据。

该 [LiveKit](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/livekit/README.md) 传输，例如，使用一个 `wss` URL，并带有一个 `access_token` 参数来建立经过身份验证的连接：

```
livekit:wss://comms.example.com?access_token=eyJhbGciOiJI...
```

作为比较， [WebSocket](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/websocket/README.md) 传输也使用一个 `wss` URL，但没有预生成的令牌。连接后需要进行身份验证流程。

#### 创建传输

由于每个 `传输` 类都会支持特定类型的 URI，因此最好有一个工厂方法，要么返回有效的 `传输` 或者立即失败。例如：

```ts
function createTransport(uriWithConnectionParams: string) {
  const type = getPrefix(urlWithConnectionParams)

  switch (type) {
    case 'ws-room': return new WsRoomTransport(uriWithConnectionParams)
    case 'livekit': return new LiveKitTransport(uriWithConnectionParams)
    // ... 其他受支持的实现...
  }

  throw new Error(`不支持的传输类型：${type}`)
}
```

客户端不必实现所有标准传输。如果他们只打算使用已定义类型的一个子集，而不打算处理所有 URI，则可以自由拒绝不支持的类型。

功能更丰富的客户端，例如 World Explorer，建议至少实现 [websocket](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/websocket/README.md) 和 [LiveKit](https://github.com/decentraland/docs/tree/main/contributor/communications/transport-types/livekit/README.md) 传输，这些目前被所有主要领域使用。

### 了解更多

前往 [特定传输类型](#types) 部分，阅读不同的 [消息类型](https://github.com/decentraland/docs/tree/main/contributor/communications/messages/README.md) 或查看 \[\[Comms Demo Station]]。


---

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