diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 62ca0f5300..29b58e6f18 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -27,6 +27,8 @@ var io; var onlineUsersMap = {}; var onlineUsers = []; +var uidToSocketId = {}; +var socketIdToUid = {}; process.on('message', function(msg) { if (typeof msg !== 'object') { @@ -42,6 +44,13 @@ process.on('message', function(msg) { if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) { onlineUsers.push(msg.uid); } + + if (Array.isArray(uidToSocketId[msg.uid])) { + uidToSocketId[msg.uid].push(msg.socketid); + } else { + uidToSocketId[msg.uid] = [msg.socketid]; + } + socketIdToUid[msg.socketid] = msg.uid; } else if(msg.action === 'user:disconnect') { var index = onlineUsers.indexOf(msg.uid); if (index !== -1) { @@ -52,6 +61,14 @@ process.on('message', function(msg) { onlineUsersMap[msg.uid] -= 1; onlineUsersMap[msg.uid] = Math.max(0, onlineUsersMap[msg.uid]); } + + if (uidToSocketId[msg.uid]) { + index = uidToSocketId[msg.uid].indexOf(msg.socketid); + if (index !== -1) { + uidToSocketId[msg.uid].splice(index, 1); + } + } + delete socketIdToUid[msg.socketid]; } }); @@ -112,7 +129,7 @@ Sockets.init = function(server) { socket.uid = parseInt(uid, 10); if (process.send) { - process.send({action: 'user:connect', uid: uid}); + process.send({action: 'user:connect', uid: uid, socketid: socket.id}); } /* If meta.config.loggerIOStatus > 0, logger.io_one will hook into this socket */ logger.io_one(socket, uid); @@ -165,7 +182,7 @@ Sockets.init = function(server) { } if (process.send) { - process.send({action: 'user:disconnect', uid: uid}); + process.send({action: 'user:disconnect', uid: uid, socketid: socket.id}); } emitOnlineUserCount(); @@ -276,14 +293,20 @@ Sockets.getUserSockets = function(uid) { }; Sockets.getUserRooms = function(uid) { - var sockets = Sockets.getUserSockets(uid); var rooms = {}; - for (var i=0; i