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

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

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

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

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

通过传输接口发送和接收的消息始终相同（见 [消息](https://github.com/decentraland/docs/blob/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` 始终采用以下形式：

```
<type>:<type connection parameters>
```

该 `<type>` 对应于上面列出的一种，而 `<type connection parameters>` 的格式取决于具体的传输。它可以是 URL、不可读令牌或任意数据。

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

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

相比之下， [Websocket](https://github.com/decentraland/docs/blob/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(`Unsupported transport type: ${type}`)
}
```

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

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

### 了解更多

前往一个 [特定传输类型](#types) 部分，阅读不同的 [消息类型](https://github.com/decentraland/docs/blob/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 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/transports.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.
