我正在使用nowjs
创建聊天应用程序。用户可以创建一个聊天室,然后邀请用户访问该会议室。以下是用于执行此操作的代码:
everyone.now.addPeopleToChat = function(clientIds,roomName) {
var length = clientIds.length;
var group = nowjs.getGroup(roomName);
for(var i=0;i<length;i++){
group.addUser(clientIds[i]);
}
group.now.roomName = roomName;//User can be in one room only at any given time.Reference to the roomname to be used when user leaves chat
group.now.loadChatWindow();//Load UI
}
到目前为止一直很好。用户可以加入聊天室,聊天也很好。
当用户在聊天室并关闭浏览器窗口时,我希望聊天室中的其他用户知道此人已离开聊天。我想使用断开连接事件来处理这种特殊情况这是我用过的代码。
nowjs.on('disconnect', function () {
console.log("User name:"+this.now.name);//comes fine
console.log("Room name:"+this.now.roomName);//shows undefined.
var group = nowjs.getGroup(this.now.roomName);//Intention is to get the room in which user was chatting.But it is not working because roomName is undefined.
group.now.broadcast(this.now.name+" has left this room");
});
为什么roomName
在disconnect
处理程序中显示为未定义。我在将用户添加到组右侧时设置该变量。因此,组中的每个用户都应该具有该变量设置正确。我在这里遗漏了什么?