From e952a873a824222e6afc52cee77340877656949c Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 28 Nov 2014 11:49:15 -0500 Subject: [PATCH] moving onlineUsers from an in-memory variable to a sorted set --- loader.js | 4 +- src/socket.io/index.js | 90 +++++++++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/loader.js b/loader.js index 0a9d6e0a05..c27691036a 100644 --- a/loader.js +++ b/loader.js @@ -133,8 +133,8 @@ Loader.addClusterEvents = function(callback) { Loader.primaryWorker = parseInt(worker.id, 10); } break; - case 'user:connect': - case 'user:disconnect': + // case 'user:connect': + // case 'user:disconnect': case 'config:update': Loader.notifyWorkers(message); break; diff --git a/src/socket.io/index.js b/src/socket.io/index.js index ced9d8b3b9..010a2743e0 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -26,43 +26,68 @@ var io; var onlineUsers = []; -process.on('message', onMessage); +// process.on('message', onMessage); + +// function onMessage(msg) { +// if (typeof msg !== 'object') { +// return; +// } + +// if (msg.action === 'user:connect') { +// if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) { +// onlineUsers.push(msg.uid); +// } +// } else if(msg.action === 'user:disconnect') { +// if (msg.uid && msg.socketCount <= 1) { +// var index = onlineUsers.indexOf(msg.uid); +// if (index !== -1) { +// onlineUsers.splice(index, 1); +// } +// } +// } +// } -function onMessage(msg) { - if (typeof msg !== 'object') { - return; - } - - if (msg.action === 'user:connect') { - if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) { - onlineUsers.push(msg.uid); - } - } else if(msg.action === 'user:disconnect') { - if (msg.uid && msg.socketCount <= 1) { - var index = onlineUsers.indexOf(msg.uid); - if (index !== -1) { - onlineUsers.splice(index, 1); - } +function onUserConnect(uid, socketid) { + db.sortedSetIncrBy('onlineUsers', 1, uid, function(err, score) { + if (err) { + return winston.error('[socket.io] Could not add socket id ' + socketid + ' (uid ' + uid + ') to the online users list.'); } - } -} -function onUserConnect(uid, socketid) { - var msg = {action: 'user:connect', uid: uid, socketid: socketid}; - if (process.send) { - process.send(msg); - } else { - onMessage(msg); - } + winston.verbose('[socket.io] Socket id ' + socketid + ' (uid ' + uid + ') connect'); + }); + // var msg = {action: 'user:connect', uid: uid, socketid: socketid}; + // if (process.send) { + // process.send(msg); + // } else { + // onMessage(msg); + // } } function onUserDisconnect(uid, socketid, socketCount) { - var msg = {action: 'user:disconnect', uid: uid, socketid: socketid, socketCount: socketCount}; - if (process.send) { - process.send(msg); - } else { - onMessage(msg); - } + // baris, I no longer use socketCount here, since the zset score is the # of connections, just FYI. + db.sortedSetIncrBy('onlineUsers', -1, uid, function(err, score) { + if (err) { + return winston.error('[socket.io] Could not remove socket id ' + socketid + ' (uid ' + uid + ') from the online users list.'); + } + + if (parseInt(score, 10) === 0) { + db.sortedSetRemove('onlineUsers', uid, function(err) { + if (err) { + winston.error('[socket.io] Could not remove uid ' + uid + ' from the online users list') + } else { + winston.verbose('[socket.io] Removed uid ' + uid + ' from the online users list, user is now considered offline'); + } + }); + } + + winston.verbose('[socket.io] Socket id ' + socketid + ' (uid ' + uid + ') disconnect'); + }); + // var msg = {action: 'user:disconnect', uid: uid, socketid: socketid, socketCount: socketCount}; + // if (process.send) { + // process.send(msg); + // } else { + // onMessage(msg); + // } } Sockets.init = function(server) { @@ -112,6 +137,9 @@ Sockets.init = function(server) { }); }); + // Clearing the online users sorted set + db.delete('onlineUsers'); + io.sockets.on('connection', function(socket) { var hs = socket.handshake, sessionID, uid;