Chat Replay
1. Feature Overview
聊天室模块(chat)Provides chat replay API for developers to integrate chat replay functionality.- Chat Replay - Basic Usage Example
2. Usage - Methods
2.1 Initialize Chat Message Replay
API Method: initChatMsgReplay(): Promise<void>
Supported from v0.3.0 This method must be executed after
setChatMsgReplayPlaybackTargetfor the chat room module to properly initialize chat replay records. During usage, the ChatEvents.ChatMsgReplayStatusChange event will be triggered.
Example:
watchCore.chat.eventEmitter.on(ChatEvents.ChatMsgReplayStatusChange, ({ status }) => {
console.info(status)
}
// 需要先执行 setChatMsgReplayPlaybackTarget 方法
await watchCore.chat.initChatMsgReplay()
2.3 Get Chat Message Replay History Data
Use getChatMsgReplayHistoryList to retrieve chat message replay history data.
API Method: getChatMsgReplayHistoryList(options: Object): Promise<ChatReplayMsgType[]>
Supported from v0.3.0
Parameter Description:
- options:
Objecttype, required. Detailed type description is as follows:
| Parameter Name | Description | Type | Required | Default Value |
|---|---|---|---|---|
mode |
Chat replay request history data mode | ChatReplayRequestHistoryMode |
Yes | - |
replayMsg |
Pass the corresponding replay message based on the specified mode. For example, to load older data, pass the first message in the current chat data. | ChatReplayMsgType |
No | - |
count |
Number of chat messages, maximum limit is 60 | number |
Yes | - |
Return Value Description: Chat message replay history data, Promise<ChatReplayMsgType[]> type.
Example:
import { ChatReplayRequestHistoryMode } from "@polyv/live-watch-sdk"
const data = await watchCore.chat.getChatMsgReplayHistoryList({
mode: ChatReplayRequestHistoryMode.Init,
count: 20
})
2.4 Process Chat Message Replay
When used externally, it should be combined with the PlayerEvents.TimeUpdate event callback from the viewer page SDK. During usage, replay messages can be obtained through the ChatEvents.ChatMessage event. Since the playback progress bar can be used to fast-forward during replay, the ChatEvents.ChatMsgReplayReload event can be used to monitor whether chat room data needs to be reloaded.
API Method: processChatMsgReplay(params: Object): void
Supported from v0.3.0
Parameter Description:
- params:
Objecttype, required. Detailed type description is as follows:
| Parameter Name | Description | Type | Required | Default Value |
|---|---|---|---|---|
currentTime |
Current playback time, in seconds | number |
Yes | - |
durationTime |
Total playback duration, in seconds | number |
Yes | - |
Example:
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. Usage - Events
3.1 Chat Message Replay Status
Event: ChatEvents.ChatMsgReplayStatusChange
Callback Parameters: Object object. Detailed type description is as follows:
| Property Name | Description | Type |
|---|---|---|
status |
Replay status | ChatMsgReplayStatus |
Example:
watchCore.chat.eventEmitter.on(ChatEvents.ChatMsgReplayStatusChange, (params) => {
console.log('聊天重放状态变更:', params.status);
});
3.2 Chat Message Replay - Reload
Description: Notifies the external system to reload chat replay data.
Event: ChatEvents.ChatMsgReplayReload
Callback Parameters: Object object. Detailed type description is as follows:
| Property Name | Description | Type |
|---|---|---|
seconds |
- | number |
Example:
watchCore.chat.eventEmitter.on(ChatEvents.ChatMsgReplayReload, () => {
// initChatMsgRender
console.log('需要重新加载聊天重放数据渲染表格');
});
3.3 Chat Message Event
Description: Triggered when a chat message is received.
Event: ChatEvents.ChatMessage
Callback Parameters: Object object. Detailed type description is as follows:
| Property Name | Description | Type |
|---|---|---|
chatMsg |
Chat message object | ChatMsgType |
isChatReplayMsg |
Whether it is a chat replay message | boolean |
Example:
// 聊天消息列表
const chatMsgList: ChatMsgType[] = [];
// 聊天消息事件
watchCore.chat.eventEmitter.on(ChatEvents.ChatMessage, (data) => {
// 插入到聊天消息列表
chatMsgList.push(data.chatMsg);
// 渲染聊天消息...
});
