消息收发
更新时间: 2024/03/15 16:12:31
参见 interface MsgServiceInterface
它的 API 都挂载在 msg 模块里,使用 nim.msg
访问
消息说明
- 点对点或群组消息: 点对点或群组消息的收发,可分为消息、离线消息、漫游消息三类。
- 离线消息、漫游消息只能收不能发送,在事件
syncOfflinemsgs
、syncRoamingmsgs
中获取 - 消息可收可发,收消息在
msg
的事件中接收
- 离线消息、漫游消息只能收不能发送,在事件
- 离线消息: 他人发给自己的消息,且自己的账号在任何客户端都未读过,则算离线消息;离线消息只要其它任何一端(包括自己)已读,则不会再收到对应消息。
- 漫游消息: 他人发给自己的消息,在本客户端未读过,但在其他客户端如iOS/android/pc...读过,则算漫游消息;漫游消息只有本端已读,才不会再下推。
消息对象
重点有几个参数
类型 | 参数名 | 说明 |
---|---|---|
string | scene | 消息场景,p2p、team、superTeam |
string | sessionId | 所属会话 id |
string | status | 来标明消息发送状态,见下文 |
string | to | 消息接收方, 账号或群id |
string | from | 消息发送方, 账号 |
number | time | 消息发送成功的时间戳(单位毫秒) |
string | type | 消息类型,见下文 |
string | body | 消息的文本部分的内容 |
object | attach | 附加消息 |
string | idClient | sdk 端测唯一标识,可做主键使用 |
string | idServer | 消息服务器测的标识 |
scene
来标明消息所属的场景, 具体场景如下:
'p2p'
(点对点消息)'team'
(群消息)'superTeam'
(超大群群消息)
type
来标明消息的类型, 与 IM 相关消息类型如下:
'text'
(文本消息)'image'
(图片消息)'audio'
(音频消息)'video'
(视频消息)'file'
(文件消息)'geo'
(地理位置消息)'custom'
(自定义消息)'tip'
(提醒消息) 提醒消息用于会话内的状态提醒,如进入会话时出现的欢迎消息,或者会话命中敏感词后的提示消息等等.'notification'
(群通知消息) 某些群操作后所有群成员会收到一条相应的群通知消息
status
来标明消息发送状态, 具体场景如下:
'delete'
(已删)'read'
(收到消息,已读)'receipt'
(对方已读发送的内容)'refused'
(被对方拒绝)'sendFailed'
(发送失败)'sending'
(发送中)'sent'
(已发送)'unread'
(收到消息,未读)
attach
来标明消息附加内容,type 为 custom,image,video,audio,file,geo 等类型存在。
- 发送图片,音视频,文件消息后的附带的内容。参见 UploadFileResult
- 群通知的具体内容。参见下文群通知消息
群通知消息
群通知消息是消息类型 type === 'notification'
的一种,每当接到群通知消息,SDK 经过处理将会上报开发者以事件,而与群通知消息有关的事件如下:
- 更新群
updateTeam
- 解散群
dismissTeam
- 群成员加入
addTeamMembers
- 更新群管理员
updateTeamManagers
- 移除群成员
removeTeamMembers
- 转让群
transferTeam
- 禁言某成员
updateTeamMembersMute
jsconst eventList = [
'updateTeam',
'dismissTeam',
'addTeamMembers',
'updateTeamManagers',
'removeTeamMembers',
'transferTeam',
'updateTeamMembersMute',
]
eventList.forEach((key: any) => {
nim.on(key, (res) => {
console.log(`收到 ${key} 事件:`, res ? JSON.parse(JSON.stringify(res)) : res);
});
})
发送消息
API 出入参请参见:
- sendTextMsg 发送文本消息
- sendImageMsg 发送图片消息
- sendAudioMsg 发送音频消息
- sendVideoMsg 发送视频消息
- sendFileMsg 发送文件消息
- sendGeoLocationMsg 发送地理位置信息
- sendTipMsg 发送提醒消息
- sendCustomMsg 发送自定义消息
示例:
js// text message
await nim.msg.sendTextMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
"body": "this is a text",
"onSendBefore": function(msg) {
console.log('Get msg before send', msg)
}
})
// tip message
await nim.msg.sendTipMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
"body": "this is a text"
})
// custom message
await nim.msg.sendCustomMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
// JSON format string
"attach": "{\"a\":1}"
})
// GEO location message
await nim.msg.sendGeoLocationMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
// JSON object
"attach": {
"lng": 120.18201069999999,
"lat": 30.2006603,
"title": "Hangzhou"
}
})
// Image message like '.jpg', '.png'
await nim.msg.sendImageMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
// Example dom for get file: <input type="file" id="fileInput" />
"file": document.getElementById('fileInput').files[0],
"onUploadStart": function(task) { console.log('Upload start!', task) },
"onUploadProgress": function(progress) { console.log('Uploading!', progress) },
"onUploadDone": function(file) { console.log('Upload done!', file) },
"onSendBefore": function(msg) { console.log('Get msg before send', msg) }
})
// Audio message like '.mp3'
await nim.msg.sendAudioMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
// Example dom for get file: <input type="file" id="fileInput" />
// 浏览器环境使用file或fileInput,uniapp,RN,小程序等环境使用filePath
"file": document.getElementById('fileInput').files[0],
"onUploadStart": function(task) { console.log('Upload start!', task) },
"onUploadProgress": function(progress) { console.log('Uploading!', progress) },
"onUploadDone": function(file) { console.log('Upload done!', file) },
"onSendBefore": function(msg) { console.log('Get msg before send', msg) }
})
// Video message like '.mp4'
await nim.msg.sendVideoMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
// Example dom for get file: <input type="file" id="fileInput" />
"file": document.getElementById('fileInput').files[0],
"onUploadStart": function(task) { console.log('Upload start!', task) },
"onUploadProgress": function(progress) { console.log('Uploading!', progress) },
"onUploadDone": function(file) { console.log('Upload done!', file) },
"onSendBefore": function(msg) { console.log('Get msg before send', msg) }
})
// File message
await nim.msg.sendFileMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
// Example dom for get file: <input type="file" id="fileInput" />
"file": document.getElementById('fileInput').files[0],
"onUploadStart": function(task) { console.log('Upload start!', task) },
"onUploadProgress": function(progress) { console.log('Uploading!', progress) },
"onUploadDone": function(file) { console.log('Upload done!', file) },
"onSendBefore": function(msg) { console.log('Get msg before send', msg) }
})
接收消息
参见事件 TS 定义 NIMInterface-IMEventInterface-msg
示例:
jsconst nim = new NIMSDK(options)
nim.on('msg', function(msg) { console.log('Receive msg: ', msg) })
await nim.connect()
重发消息
一般在 msg.status
为 sendFailed
时的失败,大概率是因为网络原因的发送失败,这时可以尝试重发。示例
jslet targetMsg = null
// Chrome devtool,network throttling Download Limit 0kb,and you will not receive the packet.
// Finally, send message failed due to timeout
try {
await nim.msg.sendTextMsg({
"scene": "p2p",
"to": "xiaodong1",
"body": "test2",
"onSendBefore": function(msg) {
console.log('Get msg before send', msg)
}
})
} catch (err) {
console.error('sendFailed', err)
targetMsg=err.msg
setTimeout(async function () {
console.log(targetMsg)
if (targetMsg && targetMsg.status === 'sendFailed') {
// 发送失败后,隔5s后重发消息
const msg = await nim.msg.resendMsg({
"ps": "recall msg",
"msg": targetMsg,
})
}
}, 5000)
}
转发消息
示例
jslet msg = await nim.msg.sendTextMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
"body": "this is a text",
})
msg = await nim.msg.forwardMsg({
"scene": "p2p",
"to": "{{Other_Receiver_ACCOUNT_ID}}",
"msg": msg,
})
撤回消息
如果消息发送失败或者消息发送者被拉黑,那么即使在可撤回时长内也无法撤回。
示例
jslet msg = await nim.msg.sendTextMsg({
"scene": "p2p",
"to": "{{Receiver_ACCOUNT_ID}}",
"body": "this is a text2",
})
msg = await nim.msg.recallMsg({
"ps": "recall msg",
"msg": msg,
})
发送已读回执
发送后能在消息对象的 status 属性找到是否被发送过已读回执
示例在收到一条消息后,立刻标记已读:
jsconst nim = new NIMSDK(options)
nim.on('msg', async function(msg) {
console.log('Receive msg: ', msg)
await nim.msg.sendMsgReceipt({
msg: msg
})
})
await nim.connect()
此文档是否对你有帮助?