# 聊天重放

## 一、功能概述

`聊天室模块(chat)` 提供聊天重放功能 Api，用于开发者集成聊天重放功能。

## 二、使用方式 - 方法

### 2.1 设置聊天消息重放对应的回放对象

外部需要传入一个回放对象，聊天重放才能获取到回放对象对应的聊天记录

**Api 方法：** `setChatMsgReplayPlaybackTarget(playbackTarget?: PlaybackTarget): void`

**参数说明：**

* playbackTarget：undefined，`PlaybackTarget` 类型，选传

**示例：**

```js
watchCore.chat.setChatMsgReplayPlaybackTarget(playbackTarget);
```

### 2.2 初始化聊天消息重放

**Api 方法：** `initChatMsgReplay(): Promise<void>`

**示例：**

```js
watchCore.chat.eventEmitter.on(ChatEvents.ChatMsgReplayStatusChange, ({ status }) => {
  console.info(status)
}

// 需要先执行 setChatMsgReplayPlaybackTarget 方法
await watchCore.chat.initChatMsgReplay()
```

### 2.3 添加聊天重放本地缓存插件

为了能够减少对聊天室接口的请求，聊天室模块在实现回放功能时，支持通过添加缓存插件，后续读取聊天重放记录时，优先从缓存中读取。

注意插件只会添加一次，添加完成后再添加不会生效，建议在 `initChatMsgReplay` 方法调用之前添加插件

**Api 方法：** `addChatMsgReplayCachePlugin(localCache: ChatMsgReplayCachePluginType): void`

**参数说明：**

* localCache：undefined，`ChatMsgReplayCachePluginType` 类型，必传，详细类型说明如下

| 参数名          | 说明 | 类型         | 必须 | 默认值 |
| ------------ | -- | ---------- | -- | --- |
| `getItem`    | -  | `Function` | 是  | -   |
| `setItem`    | -  | `Function` | 是  | -   |
| `removeItem` | -  | `Function` | 是  | -   |
| `iterate`    | -  | `Function` | 是  | -   |

**示例：**

```js
// 以 localforage 库为例
import localForage from 'localforage'
watchCore.chat.addChatMsgReplayCachePlugin({
   getItem: localForage.getItem,
   setItem: localForage.setItem,
   removeItem: localForage.removeItem,
   iterate: localForage.iterate,
 });
```

### 2.4 获取聊天消息重放历史数据

通过 `getChatMsgReplayHistoryList` 来获取聊天消息重放的历史数据

**Api 方法：** `getChatMsgReplayHistoryList(options: Object): Promise<ChatReplayMsgType[]>`

**参数说明：**

* options：undefined，`Object` 类型，必传，详细类型说明如下

| 参数名         | 说明                                          | 类型                             | 必须 | 默认值 |
| ----------- | ------------------------------------------- | ------------------------------ | -- | --- |
| `mode`      | 聊天重放请求历史数据模式                                | `ChatReplayRequestHistoryMode` | 是  | -   |
| `replayMsg` | 根据指定的模式传入对应的重放消息，比如需要加载旧数据，则传入当前聊天数据中的第一条消息 | `ChatReplayMsgType`            | 否  | -   |
| `count`     | 聊天数量，限制最大为 60                               | `number`                       | 是  | -   |

**返回值说明：** 聊天消息重放历史数据，`Promise<ChatReplayMsgType[]>` 类型

**示例：**

```ts
import { ChatReplayRequestHistoryMode } from "@polyv/live-watch-sdk"
const data = await watchCore.chat.getChatMsgReplayHistoryList({
   mode: ChatReplayRequestHistoryMode.Init,
   count: 20
})
```

### 2.5 处理聊天消息重放

外部使用时，需要结合观看页 SDK 的 [PlayerEvents.TimeUpdate](https://git.polyv.net/help-center/document-center/-/blob/master/live/js/new_sdk/live_watch_sdk/articles/modules/player/module-events.md?id=eventdoc_playerevents_timeupdate) 事件回调来一起使用，使用过程中，可以通过 [ChatEvents.ChatMessage](https://git.polyv.net/help-center/document-center/-/blob/master/live/js/new_sdk/live_watch_sdk/articles/modules/chat/module-events.md?id=eventdoc_chatevents_chatmessage)\` 事件来获取重放的消息 由于回放时可以通过播放器进度条快速跳转，此时可以通过 [ChatEvents.ChatMsgReplayReload](https://git.polyv.net/help-center/document-center/-/blob/master/live/js/new_sdk/live_watch_sdk/articles/modules/chat/module-events.md?id=eventdoc_chatevents_chatmsgreplayreload) 事件来监听是否需要重新加载聊天室数据

**Api 方法：** `processChatMsgReplay(params: Object): void`

**参数说明：**

* params：undefined，`Object` 类型，必传，详细类型说明如下

| 参数名            | 说明          | 类型       | 必须 | 默认值 |
| -------------- | ----------- | -------- | -- | --- |
| `currentTime`  | 当前播放时间，单位：秒 | `number` | 是  | -   |
| `durationTime` | 播放总时长，单位：秒  | `number` | 是  | -   |

**示例：**

```js
watchCore.chat.eventEmitter.on(ChatEvents.ChatMessage, ({ chatMsg })=> {
   console.info(chatMsg);
})

watchCore.chat.eventEmitter.on(ChatEvents.ChatMsgReplayReload, ()=> {
   console.info("重新加载聊天室数据");
})

watchCore.player.eventEmitter.on(PlayerEvents.TimeUpdate, params => {
   watchCore.chat.processChatMsgReplay(params);
});

```

## 三、使用方式 - 事件

### 3.1 聊天消息重放状态

**Event 事件：** `ChatEvents.ChatMsgReplayStatusChange`

**回调参数：** `Object` 对象，详细类型说明如下

| 属性名      | 说明   | 类型                    |
| -------- | ---- | --------------------- |
| `status` | 重放状态 | `ChatMsgReplayStatus` |

**示例：**

```js
watchCore.chat.eventEmitter.on(ChatEvents.ChatMsgReplayStatusChange, (params) => {
  console.log('聊天重放状态变更：', params.status);
});
```

### 3.2 聊天消息重放-重新加载

**说明：** 通知外部重新加载聊天重放的数据

**Event 事件：** `ChatEvents.ChatMsgReplayReload`

**示例：**

```js
watchCore.chat.eventEmitter.on(ChatEvents.ChatMsgReplayReload, () => {
  // initChatMsgRender
  console.log('需要重新加载聊天重放数据渲染表格');
});
```

### 3.3 聊天消息事件

**说明：** 当收到聊天消息后触发该事件

**Event 事件：** `ChatEvents.ChatMessage`

**回调参数：** `Object` 对象，详细类型说明如下

| 属性名       | 说明     | 类型            |
| --------- | ------ | ------------- |
| `chatMsg` | 聊天消息对象 | `ChatMsgType` |

**示例：**

```js
// 聊天消息列表
const chatMsgList: ChatMsgType[] = [];

// 聊天消息事件
watchCore.chat.eventEmitter.on(ChatEvents.ChatMessage, (data) => {
// 插入到聊天消息列表
  chatMsgList.push(data.chatMsg);
  // 渲染聊天消息...
});
```


---

# Agent Instructions: 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:

```
GET https://polyv.gitbook.io/document/docs/live/js/new-sdk/live-watch-sdk/articles/modules/chat/chat-replay.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
