2017-04-20 65 views
1

this guide的帮助下,我试图建立一个套接字聊天。这几乎完成,但用户无法加入其他人的房间。下面的代码是我“加入房间功能”:如何加入套接字聊天室

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}); 
    } 
     } 
    }); 
} 
}); 

当用户试图加入别人的房间,功能停止在该通知的用户已经在一个房间里的“更新”。这是为什么?

+0

指南的链接在哪里?我看到的只是? – DiderDrogba344

+0

对不起。这还是一大早在这里:-)我已经添加它 – JonasSH

+0

它停在用户已经在一个房间的原因是因为人[client.id] .inroom不为空。尝试添加console.log(people [client.id] [“room”]);在console.log后面(“用户已经在房间里了”);并在这里发布它打印的内容。 – DiderDrogba344

回答

1

问题是!==。

变化

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

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

未定义!== NULL是给你真实的,但你基本上要未定义的行动,如果它是等同于零。 !==实际上将它们视为单独的,而!=会将它们视为等同的,这就是你想要的。

+0

它完美的工作!谢谢@ DiderDrogba344。我不认为修复会如此简单:-) – JonasSH

+0

np!很高兴我能帮忙! :) – DiderDrogba344