> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/compatibility-and-requirements/network-and-api-requirements.md).

# 网络和 API 要求

说明 Unity 项目在通过互联网连接到 Convai 之前必须允许哪些网络主机、端口和防火墙规则。

Convai Unity SDK 在运行时需要访问外部互联网。语音、语言理解和文本转语音功能通过 HTTPS 和 LiveKit WebRTC 由 Convai 提供，不支持离线或局域网模式。准备企业网络、验证防火墙允许列表，或确认 Play 模式会话已连接到正确的 LiveKit 房间时，请参阅本页。

### 必需的出站访问

运行时会话使用两个 Convai 端点，以及连接响应中返回的 LiveKit 主机。请允许运行 Unity 的计算机向以下主机发送出站流量。

#### Convai 端点

| 主机                                                           | 端口    | 协议    | 用途                          |
| ------------------------------------------------------------ | ----- | ----- | --------------------------- |
| <code class="expression">space.vars.live\_server\_url</code> | `443` | HTTPS | 连接 API — 会话设置和房间凭据          |
| `api.convai.com`                                             | `443` | HTTPS | 角色元数据、REST API 和 WebGL 情绪预检 |

#### LiveKit 最低要求

Convai 接受连接请求后，SDK 会使用响应中的 `room_url` 和 `token` 加入 LiveKit 房间。连接 LiveKit Cloud 所需的最低出站规则：

| 主机                                           | 端口    | 协议  | 用途                    |
| -------------------------------------------- | ----- | --- | --------------------- |
| `convai-technologies-lfslae7c.livekit.cloud` | `443` | TCP | Convai LiveKit 信令端点   |
| `*.livekit.cloud`                            | `443` | TCP | LiveKit WebSocket 信令  |
| `*.turn.livekit.cloud`                       | `443` | TCP | UDP 被阻止时的 TURN/TLS 回退 |

#### 可选的直接媒体路径

LiveKit 为直接 WebRTC 媒体传输和 TCP 回退列出了以下路径。网络策略允许 UDP，或者 LiveKit 连接测试表明仅配置最低 TCP 规则还不够时，请添加这些规则。

| 主机                     | 端口              | 协议  | 用途            |
| ---------------------- | --------------- | --- | ------------- |
| `*.host.livekit.cloud` | `3478`          | UDP | TURN/UDP 连接   |
| 所有 LiveKit 主机          | `50000`–`60000` | UDP | WebRTC 媒体传输   |
| 所有 LiveKit 主机          | `7881`          | TCP | WebRTC TCP 回退 |

客户端计算机无需开放任何入站端口。完整的 LiveKit 防火墙参考信息，请参阅 LiveKit 文档中的 [配置防火墙](https://docs.livekit.io/deploy/admin/firewall/) 。

### 实时会话的连接方式

每个角色会话都遵循以下流程：

```mermaid
sequenceDiagram
    参与方 Unity，作为 Unity SDK
    参与方 Convai，作为 Convai
    参与方 LiveKit，作为 LiveKit 房间

    Unity->>Convai: 使用 API key 发送 POST /connect
    Convai-->>Unity: session_id、room_url、room_name、token
    Unity->>LiveKit: 使用 token 加入 room_url 中的房间
    LiveKit-->>Unity: 音频、视频和数据流
```

| 连接响应字段                 | 运行时用途                                                                      |
| ---------------------- | -------------------------------------------------------------------------- |
| `session_id`           | 实时会话的 Convai 会话 ID                                                         |
| `character_session_id` | 重新连接时保持对话连续性                                                               |
| `room_url`             | LiveKit WebSocket 端点，例如 `wss://convai-technologies-lfslae7c.livekit.cloud` |
| `room_name`            | 客户端加入的 LiveKit 房间名称                                                        |
| `token`                | 用于加入房间的临时 LiveKit 房间令牌                                                     |

SDK 会读取 `room_url` （来自连接响应），并将其传递给传输层。它不会硬编码 LiveKit 主机名。示例主机 `convai-technologies-lfslae7c.livekit.cloud` 是 Convai 的一个部署实例；同一账户的日志中，随时间推移可能会显示不同的 `room_url` 。

### 身份验证

运行时会话涉及两种凭据。

#### API 密钥

项目 API 密钥用于验证向 Convai 发出的请求。请将其存储在 **编辑 > 项目设置 > Convai SDK**.

| 请求类型                   | 标头               | 用途                    |
| ---------------------- | ---------------- | --------------------- |
| 连接（`/connect`)         | `X-API-Key`      | 启动实时会话                |
| REST（`api.convai.com`) | `CONVAI-API-KEY` | 角色元数据、长期记忆及其他 REST 调用 |

SDK 会自动发送 API 密钥。无需手动将密钥粘贴到连接请求负载中。

#### LiveKit 房间令牌

每个成功的连接响应都会包含一个临时的 `token`。加入房间时，SDK 会将此令牌传递给 LiveKit。该令牌有效期很短，且仅授予对该房间的访问权限。请像对待密码一样妥善保管——不要在公开论坛、支持工单或版本控制中分享。

### 在日志中查找连接详情

会话开始时，SDK 会在 **调试** 级别下的 **传输** 类别中记录房间凭据。查找这些日志行之前，请启用详细传输日志记录。

1. 打开 **编辑 > 项目设置 > Convai SDK > 诊断**.
2. 将 **全局日志级别** 设为 `Debug`，或者展开 **类别覆盖** 并添加覆盖项，设置 **类别** `Transport` 并 **级别** `Debug`.
3. 进入播放模式并启动角色会话。
4. 打开 Unity 控制台，并按以下内容筛选： `Transport`.

你应该会看到以下日志格式之一：

```
[Debug][Transport]: Room details received: {"token":"...","room_name":"...","session_id":"...","room_url":"..."}
```

如果 JSON 行难以阅读，请搜索 `Token:` —— SDK 也会打印易读的摘要：

```
[Debug][Transport]: Token: ...; Room Name: ...; Room URL: ...; Session ID: ...; Character Session ID: ...
```

| 日志字段                   | 含义                         |
| ---------------------- | -------------------------- |
| `token`                | LiveKit 房间令牌               |
| `room_name`            | LiveKit 房间名称 / 房间 ID       |
| `room_url`             | LiveKit URL / WebSocket 端点 |
| `session_id`           | Convai 会话 ID               |
| `Character Session ID` | 用于保持对话连续性的 Convai 角色会话 ID  |

{% hint style="danger" %}
请勿公开分享房间令牌。它是临时的，可用于访问 LiveKit 房间。将 `token` 值脱敏后，再将日志发布到支持渠道或公开问题跟踪器。
{% endhint %}

除非定义了 `CONVAI_DEBUG_LOGGING` ，否则非开发版本会移除调试日志调用。默认情况下，Unity 编辑器和开发版本中会显示传输调试日志。有关日志配置，请参阅 [调试工具参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/troubleshooting/debug-tools-reference.md) 。

### 测试 LiveKit 连接

当你需要确认受限网络能否连接到 Convai 返回的房间时，请使用 [LiveKit 连接测试](https://livekit.com/webrtc/connection-test) 。

1. 启动 Unity 会话，并找到 `Token:` 日志行。
2. 将 `Room URL` 值复制到 **LiveKit URL**.
3. 将 `Token` 值复制到 **房间令牌**.
4. 选择 **运行测试**.

| 连接测试字段          | Convai 日志中的值            |
| --------------- | ----------------------- |
| **LiveKit URL** | `room_url` / `Room URL` |
| **房间令牌**        | `token` / `Token`       |

此测试会验证该临时房间的 LiveKit 信令和 WebRTC 连接能力，但不会验证你的 Convai API 密钥， `api.convai.com`，或角色配置。

### 防火墙验证清单

在部署到受限环境之前，请与网络或 IT 团队一起逐项检查此清单。

1. 确认出站 TCP 连接 `443` 至 <code class="expression">space.vars.live\_server\_url</code> 以及 `api.convai.com`.
2. 确认出站 TCP 连接 `443` 至 `convai-technologies-lfslae7c.livekit.cloud`, `*.livekit.cloud`，以及 `*.turn.livekit.cloud`.
3. 允许 UDP 时，放行 UDP `3478` 至 `*.host.livekit.cloud` 以及出站 UDP `50000`–`60000`.
4. 如果代理会对 HTTPS 或 WSS 流量执行中间人解密，请将 Convai 和 LiveKit 主机名排除在 TLS 检查之外。
5. 进入播放模式，启用 **传输** 日志，级别设为 **调试**，并确认出现 `Room details received` 或 `Token:` 行，其中包含 `wss://` `room_url`.
6. 确认角色进入 **已连接** 状态，并能响应语音或文本输入。

如果步骤 5 或 6 失败，并出现 `transport.ice_failed` 或 `transport.signal_disconnected`，请参阅 [连接和 API 问题](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/troubleshooting/connection-and-api-issues.md).

### 故障排查

| 症状                                                          | 可能原因                       | 解决方法                                                                                               | 验证                                            |
| ----------------------------------------------------------- | -------------------------- | -------------------------------------------------------------------------------------------------- | --------------------------------------------- |
| `connection.network_error` 或 `connection.timeout`           | Convai 端点的 TCP 连接被阻止 `443` | 放行 <code class="expression">space.vars.live\_server\_url</code> 以及 `api.convai.com`                | 会话进入 `Connected` 状态                           |
| `transport.ice_failed` 或 `transport.peer_connection_failed` | LiveKit UDP 或 TURN 主机被阻止   | 添加本页中的 LiveKit 最低规则以及可选的直接媒体传输路径                                                                   | `Room details received` 日志出现；角色在 Play 模式下做出响应 |
| 否 `Room details received` 日志行                               | 下方的传输日志 **调试**             | 打开 **诊断** 在“项目设置”中；将 **传输** 类别覆盖设为 **调试**                                                          | 可读 `Token:` 该行出现在控制台中                         |
| 连接成功后，LiveKit 连接测试失败                                        | UDP 媒体或 TURN 回退路径被阻止       | 允许 UDP `50000`–`60000`, UDP `3478` 至 `*.host.livekit.cloud`, 以及 TCP `443` 至 `*.turn.livekit.cloud` | LiveKit 连接测试成功，并记录了 `Room URL` 和 `Token`      |
| `connection.connect_invalid_api_key`                        | API 密钥无效或已撤销               | 从 [Convai 控制面板](https://convai.com) 复制新的密钥到“项目设置”中                                                 | 连接错误不再出现                                      |
| WebGL 麦克风不可用                                                | 通过 HTTP 提供构建版本             | 通过 HTTPS 或从 `localhost`                                                                            | 浏览器中出现麦克风权限提示                                 |

{% hint style="warning" %}
对 HTTPS 或 WSS 流量执行 TLS 检查的代理服务器可能会导致 LiveKit 信令中断。如果环境使用企业代理，请将 Convai 和 LiveKit 主机名排除在 TLS 检查之外。
{% endhint %}

### WebGL 部署

WebGL 构建必须通过 HTTPS 提供，或从 `localhost`提供。由于浏览器安全策略，HTTP 部署会阻止麦克风访问。WebGL 使用与桌面构建相同的 Convai 和 LiveKit 端点。请参阅 [平台支持矩阵](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/compatibility-and-requirements/platform-support-matrix.md) 和 [WebGL 部署指南](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/platform-guides/webgl.md) ，了解特定浏览器的限制。

### 后续步骤

确认网络要求后，安装 SDK 或查看连接错误代码。

{% content-ref url="/pages/f1f8700aca8f1ecef5696ae3f482c57ceae1064c" %}
[安装 Convai Unity SDK](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/getting-started/installation.md)
{% endcontent-ref %}

{% content-ref url="/pages/07c0ce6cfe225bcb17f8209c5187483d2497d1a4" %}
[连接与 API 问题](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/troubleshooting/connection-and-api-issues.md)
{% endcontent-ref %}


---

# 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.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unity-sdk/compatibility-and-requirements/network-and-api-requirements.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.
