文档导航
Web
SDK 版本:  5.X
公告:融云新文档中心已上线,欢迎到新文档中心阅读 Web IMLibWeb IMKit 的文档。

插入消息(Electron)

更新时间:2023-10-27 PDF

本文档仅适用于 Electron 解决方案,仅限于配合 Electron 模块 (@rongcloud/electron@rongcloud/electron-renderer)使用。

SDK 支持在 Electron 的本地数据库中插入消息。本地插入的消息不会实际发送给服务器和对方。

插入单条消息

在本地会话中插入一条发送方向或接收方向的消息。消息进入本地数据库,但不会发送给服务器和对方。不支持聊天室会话类型。以下示例使用 electronExtension.insertMessage 方法,插入 IAReceivedMessage

const conversation = {
  conversationType: RongIMLib.ConversationType.PRIVATE,
  targetId: "<目标用户ID>"
}

const message = {
  senderUserId: '<userId>',
  messageType: RongIMLib.MessageType.TEXT,
  content: {
      content: '插入一条消息'
  },
  messageDirection: RongIMLib.MessageDirection.SEND
}

RongIMLib.electronExtension.insertMessage(conversation, message).then(res => {
  if (res.code === RongIMLib.ErrorCode.SUCCESS) {
    // 消息插入成功,返回 IAReceivedMessage 类型的消息数据。
    console.log('消息插入成功', res.data)
  } else {
    console.log('消息插入失败', res.code, res.msg)
  }
})
              
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

  • 插入 IAReceivedMessage 时,可通过 messageDirection 控制消息方向。
  • 从 5.6.1 开始,SDK 支持插入 BaseMessage。插入 BaseMessage 时不支持控制消息方向,SDK 默认插入发送方向的消息。
  • 插入本地数据库的消息如需支持消息扩展功能(详见消息扩展),必须使用 IAReceivedMessage,并打开消息的可扩展属性(canIncludeExpansion)。
参数 类型 说明
conversation IConversationOption 会话
message BaseMessage(推荐) 或 IAReceivedMessage 插入的消息。从 5.6.1 开始支持插入 BaseMessage
options IInsertOptions 选项

批量插入消息

提示
  • 从 5.7.2 版本开始支持该功能。
  • 每次最多插入 500 条消息,不支持聊天室和超级群。

在本地数据库批量插入消息。


const messages = [{
  conversationType: RongIMLib.ConversationType.PRIVATE,
  targetId: '<目标用户ID>'
  senderUserId: '<userId>',
  messageType: RongIMLib.MessageType.TEXT,
  content: {
      content: '插入一条消息'
  },
  messageDirection: RongIMLib.MessageDirection.SEND,
  messageUId: '<消息唯一值>',

}]

const checkDuplicate = false

RongIMLib.electronExtension.batchInsertMessage(messages, checkDuplicate).then(res => {
  if (res.code === RongIMLib.ErrorCode.SUCCESS) {
    // 消息插入成功
    console.log('批量插入成功', res.data)
  } else {
    // 如果消息格式不正确,res.msg 返回该消息在数组中的下标号
    console.log('批量插入失败', res.code, res.msg)
  }
})
              
已复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

参数 类型 说明
messages IInsertMessage[] 需要批量插入的消息数组
checkDuplicate Boolean 消息是否排重
文档反馈
意见反馈

您的改进建议

意见反馈

问题类型

联系我们

提交工单

技术支持|集成使用|产品方案


商务咨询

7 x 24 小时

为您解答方案与报价问题

131 6185 6839

文档反馈