实时监听用户上下线状态变化
本特性为高级功能,默认不开通,付费应用,可以在应用详情里打开。
每个客户端收到一次event,扣减消息量 1条
var onUserPresence = function (event) {
//被监听的用户有上线或者下线行为,就会触发本方法
//单聊用户上下线event示例
// {
// "time": 1592807533798,
// "action": "online",
// "userId": "user002",
// "userData": "{\"avatar\":\"/images/Avatar-2.png\",\"name\":\"Wallace\"}"
// }
console.log("online/offline event:" + JSON.stringify(event));
};
//接受用户上下线状态变化
im.on(GoEasyIM.EVENT.USER_PRESENCE, onUserPresence);
var userIds = ["user001"];//要监听的用户userId,一次可以监听多个
//监听用户上下线提醒
var promise = im.subscribeUserPresence(userIds);
promise.then(function () {
console.log("Subscribe user presence successfully.");
}).catch(function (error) {
console.log("Failed to subscribe user presence, code:" + error.code + " content:" + error.content);
});
var onGroupPresence = function (event) {
//{"groupId":"group001","onlineAmount":"23","time":1591689232383,"action":"join","userId":"user001","userData":"{}"}
console.log("Group User online/offline event:" + JSON.stringify(event));
};
//接收群成员上下线事件
im.on(GoEasyIM.EVENT.GROUP_PRESENCE, onGroupPresence);
var groupIds = ["group001"];
//订阅群成员上下线,一次可以订阅多个群
var promise = im.subscribeGroupPresence(groupIds);
promise.then(function () {
console.log("Subscribe group presence successfully.");
}).catch(function (error) {
console.log("Failed to subscribe group presence, code:" + error.code + " content:" + error.content);
});