Web 超级群频道消息用法
本页给出 Web 端发送/读取超级群频道消息的完整用法:options.channelId、Message.channelId、按频道过滤历史,以及 IMKit 的频道视图入口(@ni-paas/im-web-kit)。路由与权限语义见 超级群频道级消息。
| 项 | 内容 |
|---|---|
| 包 / 模块 | @ni-paas/im-web |
| IMKit | @ni-paas/im-web-kit |
| 调用风格 | Promise<RcResult<T>>(`res.code !== 0` 为业务失败);IMKit `ChatView` 用 props 传频道 |
| 频道消息条件 | conversationType=10 + options.channelId(空白 = 群级消息) |
| 消息模型 | Message.channelId?: string(仅频道消息出现) |
版本前提
需要 SDK/服务端包含 rev23(options.channelId、Message.channelId)。
未升级时:客户端会忽略/拒绝 channelId,或服务端按旧语义处理;请先升级服务端,再升级客户端。
未升级的读取端仍可读频道消息(忽略未知字段,按群级展示),但这意味着私有频道内容不会在旧端隔离。
发送频道消息
JavaScript
import { createClient, ConversationType } from '@ni-paas/im-web';
const client = createClient({ appkey, serverUrl });
await client.connect(token);
// 1) 发送频道消息:conversationType=10 + options.channelId(消息体回填 channelId)
const res = await client.sendTextMessage(
ConversationType.SUPER_GROUP,
'g_1001',
'频道消息:今晚 8 点发布',
{ channelId: 'ch_news' },
);
if (res.code !== 0) throw new Error(`${res.code} ${res.msg}`);
console.log(res.data.channelId); // ch_news
// 任意内容类型同理:sendMessage(10, groupId, content, { channelId, clientMsgId? })
// channelId 省略 / 空白 = 群级消息(消息体不出现 channelId)读取历史与渲染
JavaScript
// 2) 历史:带 channelId 只取该频道;不带则取群级 + 本人可见的频道消息
const channelHistory = await client.getHistoryMessages(
ConversationType.SUPER_GROUP,
'g_1001',
{ channelId: 'ch_news', limit: 20 },
);
const groupHistory = await client.getHistoryMessages(ConversationType.SUPER_GROUP, 'g_1001', { limit: 20 });
// 3) 渲染时按消息自带 channelId 分组
for (const message of groupHistory.data.messages) {
if (message.channelId) renderChannelMessage(message.channelId, message);
else renderGroupMessage(message);
}
// 4) 事件:MESSAGES 里的 message.channelId 同上;未读仍按群会话
client.addEventListener('MESSAGES', ({ messages }) => {
for (const m of messages) console.log(m.channelId || "(群级)");
});IMKit 频道视图
JavaScript
// 5) IMKit:ChatView 传 channelId 后,历史/发送/事件都按该频道收窄
import { ChatView } from '@ni-paas/im-web-kit';
const chat = new ChatView({
lib: client,
conversationType: ConversationType.SUPER_GROUP,
targetId: 'g_1001',
channelId: 'ch_news',
currentUserId: me,
});
chat.render(document.getElementById("chat"));
// 切换群/频道:switchConversation(conversationType, targetId, channelId)
await chat.switchConversation(ConversationType.SUPER_GROUP, 'g_1002', 'ch_dev');接口与错误码
| 操作 | 入参 | 返回 | 错误码 |
|---|---|---|---|
sendMessage | conversationType:ConversationType, targetId:string, content:Content, options:{clientMsgId?:string, channelId?:string}? | Message | 1001 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1016 1021 3001 3002 |
sendTextMessage | conversationType:ConversationType, targetId:string, text:string, options:{clientMsgId?:string, mentionedInfo?:MentionedInfo, quote?:Quote, channelId?:string}? | Message | 1001 1007 1009 1010 1011 3001 3002 |
getHistoryMessages | conversationType:ConversationType, targetId:string, options:{limit?:uint32, beforeSeq?:uint64, channelId?:string}? | {messages:Message[], hasMore:bool} | 1001 1003 |
| 错误码 | 名称 | 含义 | 可重试 |
|---|---|---|---|
1001 | INVALID_PARAMETER | 参数错误 | 否 |
1003 | FORBIDDEN | 无权限 | 否 |
1005 | GROUP_NOT_FOUND | 群组不存在 | 否 |
1006 | NOT_GROUP_MEMBER | 未加入群组 | 否 |
限制
- 频道消息仍属于群会话(10 + groupId):未读/回执/免打扰按群会话聚合,不会出现独立频道会话行。
- 私有频道发送/读取要求频道成员(1003);公有频道群成员发送会自动加入频道。
- typing 未按频道收窄;客户端搜索暂未按频道裁剪(Open API 搜索已过滤)。
- IMKit 只提供单频道会话视图,频道列表/成员管理页需宿主实现(Electron 无 IMKit)。
- 存量 rev22 群级消息(无 channelId)永远按群级处理。