I am having trouble with having the first user of the room I am trying to communicate to always leaving but this only happens when I check for its id.
socket.on('player moved', function(msg) {
var curr_room = msg.room_number;
var room_data = io.sockets.adapter.rooms[curr_room];
var temp_room = [];
console.log(room_data);
for (var id in room_data) {
temp_room.push(io.sockets.adapter.nsp.connected[id]);
}
var player1_id = temp_room[0].id;
var player2_id = temp_room[1].id;
console.log(player1_id);
console.log(player2_id);
console.log(msg.player_id);
if (player1_id && player2_id) {
if (player1_id === msg.player_id) {
temp_room[1].emit('update coords', "dude");
} else if ("ffff" === msg.player_id) {
console.log('tessting to see wheter i can be in')
temp_room[0].emit('update coords', "sweet");
}
}
So this works without any errors and when I console.log room data I see two sockets which is what I want, however if I change "ffff" to player2_id I get this weird error where for the first iteration of this function I get the two sockets but after that the first player disconnects and I'm left with
var player1_id = temp_room[0].id;
^
TypeError: Cannot read property 'id' of undefined
I have absolutely no idea why having this check would make the user disconnect and furthermore I have no idea why the user is disconnecting if the page remains open.
Anybody have any insights, it would be greatly appreciated! Thanks!!