Like
1. Feature Overview
聊天室模块(chat) provides Like feature APIs for developers to integrate the like functionality.
2. Usage - Methods
2.1 Get Real-time Like Count
After receiving a user like event, this method can be used to obtain the real-time like count. Monitor like count changes via the ChatEvents.ChatLikeCountChange event.
API Method: getRealtimeLikes(): number
Example:
const realtimeLikes = watchCore.chat.getRealtimeLikes();
console.log('实时点赞数:', realtimeLikes);
2.2 Send Like Count
API Method: sendLike(times: number): Promise<number>
Parameter Description:
- times: Like count, type
number, required
Return Value Description: Type Promise<number>
// 当前点赞数
let currentRealtimeLikes = 0;
// 待发送的点赞数
let waitSendLikes = 0;
/** 处理点击点赞按钮 */
function onClickLikeButton() {
currentRealtimeLikes++;
waitSendLikes++;
}
/** 将待发送的点赞数发送到服务端 */
async function toSendLike() {
if (!waitSendLikes) {
return;
}
try {
const realtimeLikes = await watchCore.chat.sendLike(waitSendLikes);
currentRealtimeLikes = realtimeLikes;
} finally {
// 清空待点赞数
waitSendLikes = 0;
}
}
// 每 5 秒中发送一次点赞,避免频繁请求
window.setInterval(() => {
toSendLike();
}, 5000);
document.querySelector('.like-button').addEventListener('click', onClickLikeButton);
3. Usage - Events
3.1 Like Count Change Event
Description: Monitor the like count change event through this event, and update the like count display on the page after the callback.
Event: ChatEvents.ChatLikeCountChange
Callback Parameters: Object object, detailed type description as follows
| Property Name | Description | Type |
|---|---|---|
realtimeLikes |
Real-time like count | number |
Example:
watchCore.chat.eventEmitter.on(ChatEvents.ChatLikeCountChange, (data) => {
console.log('实时点赞数:', data.realtimeLikes);
});
3.2 Like Event
Description: Monitor user like events through this event.
Event: ChatEvents.ChatLike
Callback Parameters: Object object, detailed type description as follows
| Property Name | Description | Type |
|---|---|---|
count |
Like count | number |
realtimeLikes |
Real-time like count | number |
userId |
User ID | string |
nick |
User nickname | string |
isSelf |
Whether it is self-like | boolean |
Example:
watchCore.chat.eventEmitter.on(ChatEvents.ChatLike, (data) => {
console.log('实时点赞数:', data.realtimeLikes);
console.log('该观众的点赞次数', data.count);
console.log('点赞的观众 id', data.userId);
console.log('点赞的观众昵称', data.nick);
});
