发送消息
本文介绍了如何从客户端发送消息,适用于单聊、群聊、聊天室。
客户端 SDK 发送消息存在频率限制,每秒最多只能发送 5 条消息。
发送消息
sendMessage 方法用于发送 IMLib 中的内置类型消息或自定义消息。
以调用 sendMessage 往群聊中发送 @张三的文本消息为例。
-
定义目标群聊会话,并实例化待发送消息。因为发送的是群聊 @ 消息,所以需要添加
mentionedInfo
。JavaScript// 定义消息投送目标会话, 这里定义一个群组类型会话
const conversation = { conversationType: RongIMLib.ConversationType.GROUP, targetId: '<目标 Id>' }
// 实例化待发送消息,RongIMLib.TextMessage 为内置文本型消息
const message = new RongIMLib.TextMessage({
// 文本内容
content: '文本内容',
// (可选)消息中附加信息,透传到对端
extra: '消息中附加信息',
// 群组消息中,如果需要发送 @ 消息,可添加 mentionedInfo 字段
mentionedInfo: {
// @ 类型:全部、个人
type: RongIMLib.MentionedType.SINGAL,
// @ 用户列表
userIdList: [zhangsan],
// @ 内容
mentionedContent: ''
}
}) -
构造发送选项 ISendMessageOptions,用于定义发送行为中的一些可选配置。因为发送的是群聊 @ 消息,必须将
options
中的isMentioned
设置为true
。JavaScript// 配置属性
const options = {
// 如果需要发送 @ 消息,isMentioned 需设置为 true
isMentioned: true,
// 消息发送前的回调,可以使用此回调返回的 message 用于列表渲 染
onSendBefore: (message) => {
console.log('消息发送前的回调', message)
}
} -
发送消息。如果消息发送成功,可以根据返回结果中的
messageId
字段将列表中的该消息状态改为发送成功。JavaScript// 发送消息
RongIMLib.sendMessage(conversation, message, options).then(res => {
if (res.code === RongIMLib.ErrorCode.SUCCESS) {
// 消息发送成功,可以根据返回结果中的 messageId 字段将列表中的该消息状态改为发送成功。
console.log('消息发送成功', res.data)
} else {
console.log('消息发送失败', res.code, res.msg)
}
}) -
如果消息发送失败,可以根据返回结果中的
messageId
字段将列表中的该消息状态改为发送失败,并重发消息。客户端遇到消息发送失败时,可能有以下情况:
- 消息未送达融云服务器,或送达融云服务端后返回发送失败的情况(其他人也没有收到)。
- 某些极端情况下(例如弱网环境),消息可能已成功送达收件方。但发送端未能及时收到融云服务端回执,超时后认为发送失败。此时客户端重发消息后会导致对方重复收到消息。关于该问题的详细解释可参考 FAQ。
-
重新发送消息。重发消息需与原始消息保持一致。
提示SDK 从 5.5.1 版本开始,支持重发消息时在
options
中传入原消息的messageId
。融云服务端不会去除重复消息,应用层可通过该 ID 自行判断消息是否属于重复消息,并进行相应处理。该 ID 可以从onSendBefore
回调返回的message
对象或返回结果中获取。JavaScript// 定义消息投送目标会话, 这里定义一个群组类型会话
const conversation = { conversationType: RongIMLib.ConversationType.GROUP, targetId: '<目标 Id>' }
// 实例化待发送消息,RongIMLib.TextMessage 为内置文本型消息
const message = new RongIMLib.TextMessage({ content: '文本内容' })
// 配置属性
const options = {
// 重发消息的 messageId, 可以从 onSendBefore 回调返回的 message 对象中 或 返回结果中获取
messageId: 0
}
RongIMLib.sendMessage(conversation, message, options).then(res => {
if (res.code === RongIMLib.ErrorCode.SUCCESS) {
// 消息发送成功,可以根据返回结果中的 messageId 字段将列表中的该消息状态改为发送成功。
console.log('消息发送成功', res.data)
} else {
// 消息发送失败,可以根据返回结果中的 messageId 字段将列表中的该消息状态改为发送失败。
console.log('消息发送失败', res.code, res.msg)
}
})
sendMessage 接口说明
sendMessage 方法接收 conversation
、message
、options
三个参数。
-
conversation
定义目标会话。详见 IConversationOption。参数 类型 说明 conversationType ConversationType 会话类型 targetId string 接收方 Id -
message
是待发送的消息内容,支持 IMLib 内置消息(例如RongIMLib.TextMessage
)实例,或者通过RongIMLib.registerMessageType()
实现的自定义消息实例。 -
options
定义发送行为中的一些可选配置,如是否可拓展,推送等。参见 ISendMessageOptions参数 类型 说明 isStatusMessageboolean (已废弃)是否为状态消息(可选项) disableNotification boolean 是否发送静默消息(可选项) pushContent string Push 信息(可选项) pushData string Push 通知携带的附加信息(可选项) isMentioned boolean 是否为 @ 消息,只当 conversationType
值为ConversationType.GROUP
时有效(可选项)mentionedType1|2
@ 消息类型,1: @ 所有人 2: @ 指定用户(可选项)已废弃mentionedUserIdListstring[]被 @ 的用户 Id 列表(可选项)已废弃directionalUserIdList string[] 用于发送群定向消息,只当 conversationType
值为ConversationType.GROUP
时有效(可选项)isVoipPush boolean 当对方为 iOS 设备且未在线时,其将收到 Voip Push. 此配置对 Android 无影响(可选项) canIncludeExpansion boolean 是否允许消息被拓展 expansion [key: string]: string 消息拓展内容数据(可选项) isFilerWhiteBlacklist boolean 黑/白名单(可选项) pushConfig IPushConfig 移动端推送配置(可选项)(与 Android、iOS 端的 MessagePushConfig 作用相似) onSendBefore Function 消息发送前的回调。(≥ 5.4.0) messageId number