> 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-pixel-streaming-embed/on-premise-deployment.md).

# 配置本地部署

将 Pixel Streaming Embed 包指向你自己的会话服务和流服务器，而不是 Convai 的，用于本地部署和自托管部署。

将嵌入内容的会话请求和串流路由到您托管的服务，请使用 `serviceUrls` 选项。当体验运行在您自己的 Pixel Streaming 服务器上时，请参阅本页。完成设置后，嵌入内容会向您的服务请求会话，并从您的服务器加载串流。

### 前提条件

* 已能正常连接 Convai 的嵌入内容。请参阅 [在 React TypeScript 应用中嵌入体验](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/react-typescript.md) 或 [使用 script 标签嵌入体验](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/cdn-umd-script.md).
* 一个会话服务和一个串流服务器，它们需要实现本页所述的请求。

### 嵌入内容如何连接到其服务

嵌入内容会发出两个请求。 `serviceUrls` 会替换每个请求的基准 URL：

| 键                 | 默认值                      | 嵌入内容发出的请求                                                                     |
| ----------------- | ------------------------ | ----------------------------------------------------------------------------- |
| `sessionFetch`    | `https://api.convai.com` | `POST <sessionFetch>/xp/streams/viewPublishedExperience` 以创建会话                |
| `pixelStreamBase` | Convai 的串流服务器            | 在 iframe 中加载该会话的串流页面，默认从以下地址加载： `https://x.convai.com/stream-v2/<sessionId>/` |

`sessionFetch` 是一个基准 URL；嵌入内容会在其后追加 `/xp/streams/viewPublishedExperience` 。

### 设置服务 URL

{% tabs %}
{% tab title="React" %}

```tsx
<PixelStreamComponent
  expId="YOUR_EXPERIENCE_ID"
  serviceUrls={{
    sessionFetch: 'https://sessions.example.com',
    pixelStreamBase: 'https://stream.example.com',
  }}
/>
```

{% endtab %}

{% tab title="JavaScript" %}

```js
const experience = new PixelStreamCore.PixelStreamClient({
  container: document.getElementById('experience'),
  expId: 'YOUR_EXPERIENCE_ID',
  serviceUrls: {
    sessionFetch: 'https://sessions.example.com',
    pixelStreamBase: 'https://stream.example.com/stream-v2/',
  },
});
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
React 组件和不依赖框架的客户端构建串流 URL 的方式不同。 `PixelStreamComponent` 会加载 `<pixelStreamBase>/stream-v2/<sessionId>/`. `PixelStreamClient` 会加载 `<pixelStreamBase><sessionId>/`，中间不添加分隔符，因此其 `pixelStreamBase` 末尾应包含指向会话的路径，并以 `/`.
{% endhint %}

### 实现会话服务

嵌入内容会向会话服务发送一个 JSON 正文：

```json
{
  "experience_id": "YOUR_EXPERIENCE_ID",
  "allocate_server": false,
  "end_user_id": "user-123"
}
```

`end_user_id` 仅当嵌入内容包含 `endUserId`时才会出现。当嵌入内容包含 `authToken`时，请求会在 `API-AUTH-TOKEN` 标头中携带该令牌。

嵌入内容会按以下方式读取响应：

| 响应                                                               | 结果              |
| ---------------------------------------------------------------- | --------------- |
| 状态 `200` ，且响应为 `{ "session": { "session_id": "<id>" } }`         | 嵌入内容会加载 `<id>`. |
| 的流页面。状态 `200` ，且响应为 `{ "status": "retry", "message": "<text>" }` | 嵌入内容会显示错误页面。    |
| 任何其他状态，且响应为 `{ "ERROR": "<text>" }`                              | 嵌入内容会显示错误页面。    |

流页面加载后，嵌入内容会向该页面发送一个 JSON 字符串： `{"type":"SESSION_ID","sessionId":"<id>"}`；如果已设置 `endUserId` ，还会发送一条携带 `END_USER_ID` 的消息，其中包含 `end_user_id` 和 `end_user_metadata`.

### 验证部署

加载页面并打开浏览器的网络面板。

{% hint style="success" %}
会话请求会发送到你的 `sessionFetch` URL，iframe 则会从你的 `pixelStreamBase` URL 加载。
{% endhint %}

### 后续步骤

{% content-ref url="/pages/87b57936662c69d8802f20f500191b181163cf02" %}
[Pixel Streaming Embed API 参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/api-reference.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-pixel-streaming-embed/on-premise-deployment.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.
