会话模块
获取实例¶
参数说明:
参数 | 类型 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|
targetId | String | 是 | 目标 id | 3.0.0 |
type | Number | 是 | 会话类型. 比如: 单聊(1)、群聊(3) | 3.0.0 |
代码示例:
// 注: im 实例通过 RongIMLib.init 获取(单个页面仅需初始化一次)
var conversation = im.Conversation.get({
targetId: 'user2',
type: RongIMLib.CONVERSATION_TYPE.PRIVATE
});
获取列表¶
参数说明:
参数 | 类型 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|
count | Number | 否 | 获取的会话列表最大数量, 不传则获取全部会话列表 | 3.0.0 |
会话数据结构:
字段名 | 类型 | 说明 |
---|---|---|
type |
Number | 会话类型 |
targetId |
String | 目标 id |
unreadMessageCount |
Number | 当前会话的未读消息数 |
latestMessage |
Object | 会话中最后一条消息 |
hasMentiond |
Boolean | 是否包含 @ 自己的消息 |
mentiondInfo |
Object | @ 信息 |
mentiondInfo 结构:
字段名 | 类型 | 说明 |
---|---|---|
type |
Number | @ 类型. 1: @ 所有人, 2: @ 部分人 |
userIdList |
Array | 被 @ 的用户 id 列表 |
代码示例:
var option = {
count: 30 // 最大获取 30 个会话列表
};
im.Conversation.getList(option).then(function(conversationList) {
console.log('获取会话列表成功', conversationList);
});
合并列表¶
参数说明:
参数 | 类型 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|
conversationList | Array | 是 | 用户当前会话列表 | 3.0.0 |
updatedConversationList | Array | 是 | 更新的会话列表. 可通过 im.watch 获取 | 3.0.0 |
注意事项
- 建议第一次获取会话列表通过
im.Conversation.getList
获取 - 当监听到会话列表变化时, 调用
im.Conversation.merge
将当前会话列表和更新会话列表合并, 获取最新会话列表
代码示例:
var conversationList = [];
im.Conversation.getList().then(function(allConversationList) {
conversationList = im.Conversation.merge({
conversationList: conversationList,
updatedConversationList: allConversationList
}); // TODO 更新会话列表界面
});
// 注: 以下为示例代码, im.watch 建议单个页面只执行一次
im.watch({
conversation: function(event) {
var updatedConversationList = event.updatedConversationList;
conversationList = im.Conversation.merge({
conversationList: conversationList,
updatedConversationList: updatedConversationList
}); // TODO 更新会话列表界面
}
});
发送消息¶
消息结构、发送消息参数详见: https://docs.rongcloud.cn//im/imlib/web-v3/message
参数说明:
参数 | 类型 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|
messageType | String | 是 | 消息标识 | 3.0.0 |
content | Object | 是 | 消息内容 | 3.0.0 |
代码示例:
var conversation = im.Conversation.get({
targetId: 'groupId',
type: RongIMLib.CONVERSATION_TYPE.GROUP
});
// 发送文本消息
conversation.send({
messageType: RongIMLib.MESSAGE_TYPE.TEXT, // 'RC:TxtMsg'
content: {
content: 'Hello RongCloud' // 发送文本内容
}
}).then(function(message){
console.log('发送文字消息成功', message);
});
撤回消息¶
参数说明:
参数 | 类型 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|
sentTime | Number | 是 | 撤回消息的发送时间 | 3.0.0 |
messageUId | String | 是 | 撤回消息的消息 id | 3.0.0 |
代码示例:
var conversation = im.Conversation.get({
targetId: 'groupId',
type: RongIMLib.CONVERSATION_TYPE.GROUP
});
conversation.recall({
sentTime: 1585638211857,
messageUId: 'BH5T-JG24-C445-IKQM'
}).then(function(message){
console.log('撤回消息成功', message);
});
删除会话¶
代码示例:
var conversation = im.Conversation.get({
targetId: 'groupId',
type: RongIMLib.CONVERSATION_TYPE.GROUP
});
conversation.destory().then(function(){
console.log('删除会话成功');
});
获取历史消息¶
参数说明:
参数 | 类型 | 必填 | 默认值 | 说明 | 最低版本 |
---|---|---|---|---|---|
timestrap | Number | 否 | 0 | 获取时间戳. 0 为从当前最新时间拉取 | 3.0.0 |
count | String | 否 | 20 | 获取条数, 范围 1 - 20 | 3.0.0 |
order | Number | 否 | 0 | 获取顺序. 0: 倒序, 1: 正序 | 3.0.0 |
代码示例:
var conversation = im.Conversation.get({
targetId: 'groupId',
type: RongIMLib.CONVERSATION_TYPE.GROUP
});
var option = {
timestrap: +new Date(),
count: 20
};
conversation.getMessages(option).then(function(result){
var list = result.list; // 历史消息列表
var hasMore = result.hasMore; // 是否还有历史消息可以获取
console.log('获取历史消息成功', list, hasMore);
});
删除历史消息¶
message 参数说明:
参数 | 类型 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|
messageUId | String | 是 | 删除的消息 id | 3.0.0 |
sentTime | Number | 是 | 删除的消息发送时间 | 3.0.0 |
messageDirection | Number | 是 | 删除的消息方向 | 3.0.0 |
代码示例:
var conversation = im.Conversation.get({
targetId: 'groupId',
type: RongIMLib.CONVERSATION_TYPE.GROUP
});
conversation.deleteMessages([
{ messageUId: '2jJ9-KU1j-OLJG-29KL', sendTime: 1580869079801, messageDirection: 1 },
{ messageUId: '8UJ9-JU9j-WSJG-92K0', sendTime: 1580869078886, messageDirection: 1 }
]).then(function(){
console.log('删除历史消息成功');
});
清除历史消息¶
参数说明:
参数 | 类型 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|
timestrap | Number | 是 | 清除时间点, 该时间之前的消息将被清除 | 3.0.0 |
代码示例:
var conversation = im.Conversation.get({
targetId: 'groupId',
type: RongIMLib.CONVERSATION_TYPE.GROUP
});
conversation.clearMessages({
timestrap: +new Date()
}).then(function(){
console.log('清除历史消息成功');
});
清除未读数¶
代码示例:
var conversation = im.Conversation.get({
targetId: 'groupId',
type: RongIMLib.CONVERSATION_TYPE.GROUP
});
conversation.read().then(function(){
console.log('清除未读数成功'); // im.watch conversation 将被触发
});
获取未读总数¶
代码示例:
im.Conversation.getTotalUnreadCount().then(function(totalUnreadCount) {
console.log('获取未读总数成功', totalUnreadCount);
});
什么是 targetId ?
说明:
targetId 为目标 id, senderUserId 为发送方 id
举例:
user1 为发送方, user2 为接收方, 对应 targetId 值如下:
user1(发送方):
会话类型 | targetId | senderUserId |
---|---|---|
单聊 | user2 | user1 |
群组 | 群组 id | user1 |
聊天室 | 聊天室 id | user1 |
user2(接收方):
会话类型 | targetId | senderUserId |
---|---|---|
单聊 | user1 | user1 |
群组 | 群组 id | user1 |
聊天室 | 聊天室 id | user1 |
内置消息说明:
详细查看融云内置消息类型结构详解