如何join套接字聊天室

在本指南的帮助下,我试图build立一个套接字聊天。 现在差不多完成了,但是用户无法join其他的房间。 下面的代码是我的“join室function”:

client.on("joinRoom", function(id) { var room = rooms[id]; if (client.id === room.owner) { client.emit("update", "You are the owner of this room and you have already been joined."); } else { room.people.contains(client.id, function(found) { if (found) { client.emit("update", "You have already joined this room."); } else { if (people[client.id].inroom !== null) { //make sure that one person joins one room at a time console.log("User is already in a room"); client.emit("update", "You are already in a room ("+rooms[people[client.id].inroom].name+"), please leave it first to join another room."); } else { room.addPerson(client.id); people[client.id].inroom = id; client.room = room.name; client.join(client.room); //add person to the room user = people[client.id]; socket.sockets.in(client.room).emit("update", user.name + " has connected to " + room.name + " room."); client.emit("update", "Welcome to " + room.name + "."); client.emit("sendRoomID", {id: id}); } } }); } }); 

当用户试图join某人的房间时,function将停止在“更新”处,以通知用户已经在房间中。 这是为什么?

问题是!==。

更改

 if (people[client.id].inroom !== null) { 

 if (people[client.id].inroom != null) { 

undefined!== null给你真正的,但你基本上想要undefined作为如果它等于null。 !==实际上将它们视为单独的,而!=会将它们视为等同的,这就是你想要的。