diff --git a/lib/rooms.js b/lib/rooms.js index 21d44c6..0648212 100644 --- a/lib/rooms.js +++ b/lib/rooms.js @@ -34,7 +34,7 @@ exports.add_to_room = function (client, room, callback) { room_users[room].add(client.username); callback(rooms[room].array()); -} +}; // Remove a client from all rooms and return the username:client // mapping for everybody in those rooms. @@ -64,7 +64,7 @@ exports.remove_from_all_rooms = function (client, callback) { //console.log('Client ' + client.username + ' (' + client.id + ') disconnected.'); delete sid_rooms[client.id]; callback(affected_clients.array()); -} +}; // Remove a client from a room and return the username:client mapping // for everybody in that room. Returns [] if the room does not exist, @@ -74,7 +74,7 @@ function remove_from_room(client, room, callback) { callback([]); return; } - + // Delete from the room rooms[room].remove(client); if (rooms[room].size() === 0) @@ -109,7 +109,7 @@ exports.room_clients_other_than_me = function(room, client) { { var clients = rooms[room]; //console.dir(clients.array()); - + clients.remove(client); //console.dir(clients.array()); return clients.array(); @@ -121,17 +121,19 @@ exports.room_clients_other_than_me = function(room, client) { }; //gets the current room of the client (assumes one room -- will select first one if in multiple) -exports.get_room = function (client) { +exports.get_room = function (client) { + var client_rooms = null; + if (sid_rooms.hasOwnProperty(client.id)) { - var client_rooms = sid_rooms[client.id].array(); + client_rooms = sid_rooms[client.id].array(); } - - if ( typeof(client_rooms) != undefined ) + + if ( client_rooms !== null ) return client_rooms[0]; else - return null -} + return null; +}; // Generic server code @@ -147,11 +149,11 @@ exports.add_to_room_and_announce = function (client, room, msg) { clients[i].json.send(msg); } }); -} +}; /* exports.on_leave_room = function (client, room) { - + remove_from_room(client, room, function(clients) { console.log(client + ' disconnected, yo'); console.log(clients); @@ -162,7 +164,7 @@ exports.on_leave_room = function (client, room) { action: 'disconnected' }); }); - + }*/ //remember that this announces to all rooms that this client was a member of @@ -174,7 +176,7 @@ exports.remove_from_all_rooms_and_announce = function (client, msg) { clients[i].json.send(msg); } }); -} +}; ////////////////////////////// // Broadcasting functions @@ -198,7 +200,7 @@ exports.broadcast_room = function(room, msg) { // Broadcast message to all the other clients that are in rooms with this client exports.broadcast_to_roommates = function (client, msg) { var roommates = new sets.Set(); - + if (sid_rooms.hasOwnProperty(client.id)) { var client_rooms = sid_rooms[client.id].array(); @@ -213,17 +215,17 @@ exports.broadcast_to_roommates = function (client, msg) { } } } - + //remove self from the set roommates.remove(client); roommates = roommates.array(); - + //console.log('client: ' + client.id + " is broadcasting to: "); - - - for (var i = 0; i < roommates.length; i++) + + + for (var k = 0; k < roommates.length; k++) { //console.log(' - ' + roommates[i].id); - roommates[i].json.send(msg); + roommates[k].json.send(msg); } -} +};