From 40fe40deac24f64fceaf077d91b97f853ed41eb7 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 13 Oct 2014 22:25:34 -0400 Subject: [PATCH] removed users:online set using websockets.getConnectedClients directly --- src/controllers/users.js | 2 +- src/socket.io/index.js | 53 +++++++++++++++++----------------------- src/user.js | 9 +++++-- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/controllers/users.js b/src/controllers/users.js index c365188bb3..bdc3fceb70 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -15,7 +15,7 @@ usersController.getOnlineUsers = function(req, res, next) { user.getUsersFromSet('users:online', 0, 49, next); }, count: function(next) { - db.sortedSetCard('users:online', next); + next(null, websockets.getConnectedClients().length); }, isAdministrator: function(next) { user.isAdministrator(uid, next); diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 6626b812e5..a334760f6c 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -98,8 +98,6 @@ Sockets.init = function(server) { Sockets.server = io; - db.delete('users:online'); - fs.readdir(__dirname, function(err, files) { files.splice(files.indexOf('index.js'), 1); @@ -140,30 +138,30 @@ Sockets.init = function(server) { if (uid) { socket.join('uid_' + uid); socket.join('online_users'); - db.sortedSetAdd('users:online', Date.now(), uid, function(err) { - async.parallel({ - user: function(next) { - user.getUserFields(uid, ['username', 'userslug', 'picture', 'status'], next); - }, - isAdmin: function(next) { - user.isAdministrator(uid, next); - } - }, function(err, userData) { - if (err || !userData.user) { - return; - } - socket.emit('event:connect', { - status: 1, - username: userData.user.username, - userslug: userData.user.userslug, - picture: userData.user.picture, - isAdmin: userData.isAdmin, - uid: uid - }); - - socket.broadcast.emit('event:user_status_change', {uid:uid, status: userData.user.status}); + + async.parallel({ + user: function(next) { + user.getUserFields(uid, ['username', 'userslug', 'picture', 'status'], next); + }, + isAdmin: function(next) { + user.isAdministrator(uid, next); + } + }, function(err, userData) { + if (err || !userData.user) { + return; + } + socket.emit('event:connect', { + status: 1, + username: userData.user.username, + userslug: userData.user.userslug, + picture: userData.user.picture, + isAdmin: userData.isAdmin, + uid: uid }); + + socket.broadcast.emit('event:user_status_change', {uid:uid, status: userData.user.status}); }); + } else { socket.join('online_guests'); socket.emit('event:connect', { @@ -179,12 +177,7 @@ Sockets.init = function(server) { socket.on('disconnect', function() { var socketCount = Sockets.getUserSocketCount(uid); if (uid && socketCount <= 1) { - db.sortedSetRemove('users:online', uid, function(err) { - if (err) { - return winston.error(err.message); - } - socket.broadcast.emit('event:user_status_change', {uid: uid, status: 'offline'}); - }); + socket.broadcast.emit('event:user_status_change', {uid: uid, status: 'offline'}); } onUserDisconnect(uid, socket.id, socketCount); diff --git a/src/user.js b/src/user.js index 5121384dbf..a4920e14da 100644 --- a/src/user.js +++ b/src/user.js @@ -230,7 +230,12 @@ var async = require('async'), User.getUsersFromSet = function(set, start, stop, callback) { async.waterfall([ function(next) { - db.getSortedSetRevRange(set, start, stop, next); + if (set === 'users:online') { + var uids = require('./socket.io').getConnectedClients(); + next(null, uids.slice(start, stop + 1)); + } else { + db.getSortedSetRevRange(set, start, stop, next); + } }, function(uids, next) { User.getUsers(uids, next); @@ -247,7 +252,7 @@ var async = require('async'), User.isAdministrator(uids, next); }, isOnline: function(next) { - db.isSortedSetMembers('users:online', uids, next); + require('./socket.io').isUsersOnline(uids, next); } }, function(err, results) { if (err) {