跳到主要内容

监听用户上下线 - IM即时通讯

监听用户上下线状态变化

IM用户上下线有两种actions

online,触发条件
  • 用户connect成功
  • 因为网络的原因,断网重连
offline,触发条件
  • 调用disconnect
  • 手动关闭窗口或者应用
  • 网路断开
        var onUserPresence = function (event) {
//被监听的用户有上线或者下线行为,就会触发本方法
//单聊用户上下线event示例
// {
// "time": 1592807533798,
// "action": "online",
// "id": "user002",
// "data": {"avatar":"/images/Avatar-2.png","name":"Wallace"}
// }
console.log("online/offline event:" + JSON.stringify(event));
};
//接受用户上下线状态变化
im.on(GoEasy.IM_EVENT.USER_PRESENCE, onUserPresence);


var userIds = ["user001"];//要监听的用户userId,一次可以监听多个

//监听用户上下线提醒
im.subscribeUserPresence({
userIds:userIds,
onSuccess: function () {
console.log("Subscribe user presence successfully.");
},
onFailed: function (error) { //连接失败
console.log("Failed to subscribe user presence, code:" + error.code + " content:" + error.content);
}
});

监听群成员上下线状态变化

IM群成员上下线有四种actions

online,触发条件
  • 用户connect成功
  • 因为网络的原因,断网重连
offline,触发条件
  • 调用disconnect、手动关闭窗口或者应用
  • 手动关闭窗口或者应用
  • 网路断开
join,触发条件
  • 用户监听群消息成功
leave,触发条件
  • 主动取消监听群消息
       var onGroupPresence = function (event) {            
//{"groupId":"group001","groupOnlineCount":"23","time":1591689232383,"action":"join","id":"user001","data": {"avatar":"/images/Avatar-2.png","name":"Wallace"}}
console.log("Group User online/offline event:" + JSON.stringify(event));
};

//接收群成员上下线事件
im.on(GoEasy.IM_EVENT.GROUP_PRESENCE, onGroupPresence);

var groupIds = ["group001"];

//订阅群成员上下线,一次可以订阅多个群
im.subscribeGroupPresence({
groupIds:groupIds,
onSuccess: function () {
console.log("Subscribe group presence successfully.");
},
onFailed: function (error) { //连接失败
console.log("Failed to subscribe group presence, code:" + error.code + " content:" + error.content);
}
});