diff --git a/public/src/forum/admin/index.js b/public/src/forum/admin/index.js index 5516c62d55..e6a8037aec 100644 --- a/public/src/forum/admin/index.js +++ b/public/src/forum/admin/index.js @@ -3,14 +3,18 @@ define('forum/admin/index', function() { var Admin = {}; - + var updateIntervalId = 0; Admin.init = function() { app.enterRoom('admin'); socket.emit('meta.rooms.getAll', Admin.updateRoomUsage); - socket.removeListener('event:meta.rooms.update', Admin.updateRoomUsage); - socket.on('event:meta.rooms.update', Admin.updateRoomUsage); + if (updateIntervalId) { + clearInterval(updateIntervalId); + } + updateIntervalId = setInterval(function() { + socket.emit('meta.rooms.getAll', Admin.updateRoomUsage); + }, 2000); $('#logout-link').on('click', function() { $.post(RELATIVE_PATH + '/logout', { @@ -93,54 +97,16 @@ define('forum/admin/index', function() { }; Admin.updateRoomUsage = function(err, data) { - - var roomData = data.rooms; - - function getUserCountIn(room) { - var count = 0; - for(var user in roomData[room]) { - if (roomData[room].hasOwnProperty(user)) { - ++count; - } - } - return count; + if (err) { + return app.alertError(err.message); } +console.log(data); + var html = 'Online Users [ ' + data.onlineRegisteredCount + ' ]
' + + 'Online Guests [ ' + data.onlineGuestCount + ' ]
' + + 'Online Total [ ' + (data.onlineRegisteredCount + data.onlineGuestCount) + ' ]
' + + 'Socket Connections [ ' + data.socketCount + ' ]'; - var active_users = $('#active_users').html(''), - total = 0; - - if(!active_users.length) { - return; - } - - - var sortedData = []; - - for (var room in roomData) { - if (room !== '') { - sortedData.push({room: room, count: roomData[room].length}); - total += roomData[room].length; - } - } - - sortedData.sort(function(a, b) { - return parseInt(b.count, 10) - parseInt(a.count, 10); - }); - - var usersHtml = ''; - for(var i=0; i " + - sortedData[i].count + " active user" + (sortedData[i].count > 1 ? "s" : "") + ""; - } - - var parent = active_users.parent(); - parent.prepend('
'); - parent.prepend('Online Total [ ' + (data.onlineRegisteredCount + data.onlineGuestCount) + ' ]'); - parent.prepend('Online Guests [ ' + data.onlineGuestCount + ' ]
'); - parent.prepend('Online Users [ ' + data.onlineRegisteredCount + ' ]
'); - - active_users.html(usersHtml); - $('#connections').html(total); + $('#active_users').html(html); }; return Admin; diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 0fdee234f4..920d8e172a 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -30,7 +30,9 @@ var onlineUsers = []; var uidToSocketId = {}; var socketIdToUid = {}; -process.on('message', function(msg) { +process.on('message', onMessage); + +function onMessage(msg) { if (typeof msg !== 'object') { return; } @@ -41,6 +43,7 @@ process.on('message', function(msg) { } else { onlineUsersMap[msg.uid]++; } + if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) { onlineUsers.push(msg.uid); } @@ -52,16 +55,18 @@ process.on('message', function(msg) { } socketIdToUid[msg.socketid] = msg.uid; } else if(msg.action === 'user:disconnect') { - var index = onlineUsers.indexOf(msg.uid); - if (index !== -1) { - onlineUsers.splice(index, 1); - } - if (onlineUsersMap[msg.uid]) { onlineUsersMap[msg.uid] -= 1; onlineUsersMap[msg.uid] = Math.max(0, onlineUsersMap[msg.uid]); } + if (msg.uid && onlineUsersMap[msg.uid] === 0) { + var index = onlineUsers.indexOf(msg.uid); + if (index !== -1) { + onlineUsers.splice(index, 1); + } + } + if (uidToSocketId[msg.uid]) { index = uidToSocketId[msg.uid].indexOf(msg.socketid); if (index !== -1) { @@ -70,8 +75,25 @@ process.on('message', function(msg) { } delete socketIdToUid[msg.socketid]; } -}); +} + +function onUserConnect(uid, socketid) { + var msg = {action: 'user:connect', uid: uid, socketid: socketid}; + if (process.send) { + process.send(msg); + } else { + onMessage(msg); + } +} +function onUserDisconnect(uid, socketid) { + var msg = {action: 'user:disconnect', uid: uid, socketid: socketid}; + if (process.send) { + process.send(msg); + } else { + onMessage(msg); + } +} Sockets.init = function(server) { var RedisStore = require('socket.io/lib/stores/redis'), @@ -128,9 +150,8 @@ Sockets.init = function(server) { } socket.uid = parseInt(uid, 10); - if (process.send) { - process.send({action: 'user:connect', uid: uid, socketid: socket.id}); - } + onUserConnect(uid, socket.id); + /* If meta.config.loggerIOStatus > 0, logger.io_one will hook into this socket */ logger.io_one(socket, uid); @@ -181,9 +202,7 @@ Sockets.init = function(server) { }); } - if (process.send) { - process.send({action: 'user:disconnect', uid: uid, socketid: socket.id}); - } + onUserDisconnect(uid, socket.id); emitOnlineUserCount(); @@ -270,6 +289,9 @@ Sockets.uidInRoom = function(uid, room) { return false; }; +Sockets.getSocketCount = function() { + return Object.keys(socketIdToUid).length; +} Sockets.getConnectedClients = function() { return onlineUsers; }; diff --git a/src/socket.io/meta.js b/src/socket.io/meta.js index f4761ccfb0..28eec77517 100644 --- a/src/socket.io/meta.js +++ b/src/socket.io/meta.js @@ -85,7 +85,7 @@ SocketMeta.rooms.getAll = function(socket, data, callback) { var userData = { onlineGuestCount: websockets.getOnlineAnonCount(), onlineRegisteredCount: websockets.getConnectedClients().length, - rooms: websockets.server.sockets.manager.rooms + socketCount: websockets.getSocketCount() }; callback(null, userData);