跳到主要内容

版本:2.X

显示用户信息

历史消息显示用户信息

您可以使用下面任意任意一种方案实现用户信息的获取与显示。

通过开发者 App Server 获取用户信息

  1. 开发者 App Server 封装获取用户信息接口。
  2. 通过 message.senderUserId 获取发送者 ID。
  3. 将发送者 id 传入 App Server 暴露的接口中,获取对应用户信息。
  4. 将用户信息展示到页面中。
plantuml
!theme materia
skinparam sequence {
ArrowFontSize 13
ParticipantFontSize 11
}
IMSDK -> App: 返回历史消息列表
App --> IMSDK: 调用获取历史消息方法
App --> AppServer: 通过消息的 message.senderUserId 获取发送者用户信息
AppServer -> App: 返回用户信息,进行展示

通过发消息携带用户信息

  1. 获取当前用户(也就是发送者)的用户信息
  2. 发消息时携带当前用户信息
  3. 展示消息时, 通过消息体内的用户信息进行展示
危险

携带的用户信息保存在消息中。如果用户修改了用户信息,已经发送的消息携带的用户信息不会同步更新。

代码示例

js
var msg = new RongIMLib.TextMessage({
content: 'hello RongCloud!',
user : { // 当前用户(发送者) 信息
"id" : "user1",
"name" : "张三",
"portrait" : "https://cdn.ronghub.com/thinking-face.png"
},
});
var conversationType = RongIMLib.ConversationType.PRIVATE;
var targetId = 'user2'; // 目标 Id

RongIMClient.getInstance().sendMessage(conversationType, targetId, msg, {
onSuccess: function (message) {
console.log('发送消息成功, 用户信息为: ', message.content.user);
},
onError: function (errorCode) {
console.log('发送消息失败', errorCode);
}
});