实时监听用户上下线状态变化
所有应用(包括免费应用)均可在我的应用 -> 查看详情 -> 高级功能 -> 在线用户查询和上下线提醒 自助启用和关闭。
增强型应用(包括免费应用),可以免费监听IM用户上下线状态变化,2021年11月起,监听IM用户上下线接口不再单独扣除消息条数。 但启用后,若使用了PubSub模块,会造成PubSub消息数的消耗。
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);
}
});
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);
}
});