Reputation: 206
With the help of this guide, I'm trying to build a socket chat. It's almost done, however, users can't join other's room. The code below is my 'joining room 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});
}
}
});
}
});
When a user tries to join someone's room, the function stops at the 'update' that informs that the user is already in a room. Why is that?
Upvotes: 1
Views: 268
Reputation: 544
The problem is !==.
Change
if (people[client.id].inroom !== null) {
to
if (people[client.id].inroom != null) {
undefined !== null is giving you true but you essentially want undefined to act as if it was equivalent to null. !== will actually see them as separate whereas != will see them as equivalent, which is what you want.
Upvotes: 1