互动消息
聊天消息收发、历史消息、消息撤回、已读管理等 IM 功能。
API 参考
fetchHistoryMessages()
拉取历史消息(支持增量分页加载,SDK 内部维护游标,不会返回重复消息)。
| 参数 | 类型 | 说明 |
|---|---|---|
| count? | number | 本次拉取条数(默认 20) |
返回值: Promise<TResult<Message[]>>
示例:
// 增量加载模式:滚动到顶部时继续拉取 / Incremental loading: fetch more on scroll-to-top
const first = await classroom.fetchHistoryMessages(20);
// ... 用户滚动到顶部 / user scrolls to top ...
const more = await classroom.fetchHistoryMessages(20);
if (more.ok && more.data.length === 0) {
// 已无更多历史消息 / No more history messages
}markAllMessagesAsRead()
标记所有消息已读,将未读计数归零。
同步更新 state.messageUnreadCount$ 为 0。
返回值: void
示例:
// 用户切换到聊天面板时调用 / Call when user switches to chat panel
onActivateChatPanel(() => classroom.markAllMessagesAsRead());markMessageAsRead()
标记指定序列号以前的消息为已读。
用于聊天列表滚动到某条消息时增量更新已读状态。
| 参数 | 类型 | 说明 |
|---|---|---|
| seq | number | 消息序列号(Message.seq 字段) |
返回值: void
示例:
// 滚动到底部时标记最新消息已读 / Mark latest as read when scrolled to bottom
onScrollToBottom(() => {
const last = classroom.state.messageList$.get().at(-1);
if (last) classroom.markMessageAsRead(last.seq);
});revokeMessage()
撤回消息(按消息 ID 撤回自己的消息)。
老师/助教如需撤回他人消息,请使用 revokeClassMessage(messageSeq)(按 IM seq 撤回,需老师/助教权限)。
| 参数 | 类型 | 说明 |
|---|---|---|
| messageId | string | 消息 ID(Message.id 字段,字符串) |
返回值: Promise<TResult>
示例:
// 撤回自己的消息 / Revoke one's own message
await classroom.revokeMessage(message.id);
// 老师撤回他人消息 / Teacher revoking another's message
await classroom.revokeClassMessage(message.seq);sendCustomMessage()
发送自定义消息到群聊(content 会被序列化为 JSON 字符串,用于业务自定义扩展)。
接收端通过 TEvent.RECV_CUSTOM_IM_MSG 事件收到原始数据,业务可自定义协议结构(如点赞动效、互动指令等)。
| 参数 | 类型 | 说明 |
|---|---|---|
| data | object | 自定义消息数据对象,将被 JSON.stringify 序列化 |
返回值: Promise<TResult>
示例:
// 发送 / Send
await classroom.sendCustomMessage({ type: 'reaction', emoji: '👏' });
// 接收 / Receive
classroom.on(TEvent.RECV_CUSTOM_IM_MSG, ({ data, fromUserId }) => {
const payload = JSON.parse(data); // { type: 'reaction', emoji: '👏' }
if (payload.type === 'reaction') showReactionAnimation(payload.emoji);
});sendDirectedMessage()
发送定向私聊消息,仅指定用户可见,不出现在群聊消息列表中。
接收端在 state.messageList$ 中可见(带 toUserId 字段),其他成员看不到该消息。
| 参数 | 类型 | 说明 |
|---|---|---|
| toUserId | string | 目标用户 ID |
| text | string | 消息文本 |
返回值: Promise<TResult>
示例:
// 老师私下提示某位学生 / Teacher privately reminds a student
await classroom.sendDirectedMessage('student_001', '请准备回答下一个问题');sendFileMessage()
发送文件消息到群聊(支持任意格式文件)。
与 uploadCourseware() 的区别:本方法只发送文件链接到聊天,不进入课件库、不触发服务端转码。
| 参数 | 类型 | 说明 |
|---|---|---|
| file | File | 文件对象 |
返回值: Promise<TResult>
示例:
const result = await classroom.sendFileMessage(file);
if (!result.ok) toast.error(result.message);sendImageMessage()
发送图片消息到群聊(支持 jpg/png/gif 等常见图片格式)。
SDK 内部完成 COS 上传 + 缩略图生成 + 群聊广播;上传进度暂不暴露。
| 参数 | 类型 | 说明 |
|---|---|---|
| file | File | 图片文件对象(通常来自 <input type="file"> 或拖拽事件) |
返回值: Promise<TResult>
示例:
// 通过 input 上传 / Upload via input
const file = (document.querySelector('input[type=file]') as HTMLInputElement).files![0];
const result = await classroom.sendImageMessage(file);
if (!result.ok) toast.error(result.message);sendTextMessage()
发送群聊文本消息,所有课堂参与者可见。
| 参数 | 类型 | 说明 |
|---|---|---|
| text | string | 消息文本内容 |
返回值: Promise<TResult>
示例:
const result = await classroom.sendTextMessage('大家好!');
if (!result.ok) toast.error(result.message);