v1.18.x
Baris Soner Usakli 11 years ago
parent fb8fbb2372
commit d54e2fba9e

@ -18,6 +18,7 @@ define(function() {
}; };
Admin.updateRoomUsage = function(err, data) { Admin.updateRoomUsage = function(err, data) {
function getUserCountIn(room) { function getUserCountIn(room) {
var count = 0; var count = 0;
for(var user in data[room]) { for(var user in data[room]) {
@ -25,27 +26,36 @@ define(function() {
} }
return count; return count;
} }
var active_users = $('#active_users'),
var active_users = $('#active_users').html(''),
total = 0; total = 0;
if(!active_users.length) { if(!active_users.length) {
return; return;
} }
active_users.html('');
var usersHtml = ''; var sortedData = [];
for (var room in data) { for (var room in data) {
if (room !== '') { if (room !== '') {
var count = getUserCountIn(room); sortedData.push({room: room, count: data[room].length});
total += count; total += data[room].length;
usersHtml += "<div class='alert alert-success'><strong>" + room + "</strong> " + count + " active user" + (count > 1 ? "s" : "") + "</div>";
} }
} }
sortedData.sort(function(a, b) {
return parseInt(b.count, 10) - parseInt(a.count, 10);
});
var usersHtml = '';
for(var i=0; i<sortedData.length; ++i) {
usersHtml += "<div class='alert alert-success'><strong>" + sortedData[i].room + "</strong> " +
sortedData[i].count + " active user" + (sortedData[i].count > 1 ? "s" : "") + "</div>";
}
active_users.html(usersHtml); active_users.html(usersHtml);
document.getElementById('connections').innerHTML = total; $('#connections').html(total);
}; };
return Admin; return Admin;

@ -75,12 +75,12 @@ var DebugRoute = function(app) {
app.get('/test', function(req, res) { app.get('/test', function(req, res) {
/*topics.getTopicPosts2(2, 0, 10, 5, function(err, data) { var db = require('./../database');
res.json(data);
})*/ db.getSortedSetRevRange('topics:recent', 0 , -1, function(err, tids) {
topics.getTopicWithPosts(2, 1, 0, -1, true, function (err, topicData) { res.json(tids);
res.json(topicData);
}); });
}); });
}); });

@ -22,12 +22,11 @@ var SocketIO = require('socket.io'),
/* === */ /* === */
var users = {},
io; var io;
Sockets.userSockets = {}; Sockets.userSockets = {};
Sockets.rooms = {};
Sockets.init = function(server) { Sockets.init = function(server) {
@ -59,9 +58,9 @@ Sockets.init = function(server) {
sessionID = socket.handshake.signedCookies["express.sid"]; sessionID = socket.handshake.signedCookies["express.sid"];
db.sessionStore.get(sessionID, function(err, sessionData) { db.sessionStore.get(sessionID, function(err, sessionData) {
if (!err && sessionData && sessionData.passport && sessionData.passport.user) { if (!err && sessionData && sessionData.passport && sessionData.passport.user) {
uid = users[sessionID] = sessionData.passport.user; uid = sessionData.passport.user;
} else { } else {
uid = users[sessionID] = 0; uid = 0;
} }
socket.uid = parseInt(uid, 10); socket.uid = parseInt(uid, 10);
@ -110,8 +109,8 @@ Sockets.init = function(server) {
} }
if (Sockets.userSockets[uid] && Sockets.userSockets[uid].length === 0) { if (Sockets.userSockets[uid] && Sockets.userSockets[uid].length === 0) {
delete users[sessionID];
delete Sockets.userSockets[uid]; delete Sockets.userSockets[uid];
if (uid) { if (uid) {
db.sortedSetRemove('users:online', uid, function(err, data) { db.sortedSetRemove('users:online', uid, function(err, data) {
}); });
@ -126,17 +125,10 @@ Sockets.init = function(server) {
emitOnlineUserCount(); emitOnlineUserCount();
for (var roomName in Sockets.rooms) { for(var roomName in io.sockets.manager.roomClients[socket.id]) {
if (Sockets.rooms.hasOwnProperty(roomName)) { updateRoomBrowsingText(roomName.slice(1));
socket.leave(roomName);
if (Sockets.rooms[roomName][socket.id]) {
delete Sockets.rooms[roomName][socket.id];
}
updateRoomBrowsingText(roomName);
}
} }
}); });
socket.on('*', function(payload, callback) { socket.on('*', function(payload, callback) {
@ -222,9 +214,10 @@ function updateRoomBrowsingText(roomName) {
function getUidsInRoom(room) { function getUidsInRoom(room) {
var uids = []; var uids = [];
for (var socketId in room) { var clients = io.sockets.clients(roomName);
if (uids.indexOf(room[socketId]) === -1) { for(var i=0; i<clients.length; ++i) {
uids.push(room[socketId]); if (uids.indexOf(clients[i].uid) === -1 && clients[i].uid !== 0) {
uids.push(clients[i].uid);
} }
} }
return uids; return uids;
@ -235,15 +228,14 @@ function updateRoomBrowsingText(roomName) {
var anonCount = 0; var anonCount = 0;
for (var i = 0; i < clients.length; ++i) { for (var i = 0; i < clients.length; ++i) {
var hs = clients[i].handshake; if(clients[i].uid === 0) {
if (hs && clients[i].state && clients[i].state.user.uid === 0) {
++anonCount; ++anonCount;
} }
} }
return anonCount; return anonCount;
} }
var uids = getUidsInRoom(Sockets.rooms[roomName]), var uids = getUidsInRoom(roomName),
anonymousCount = getAnonymousCount(roomName); anonymousCount = getAnonymousCount(roomName);
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, users) { user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, users) {

@ -84,18 +84,6 @@ SocketMeta.rooms.enter = function(socket, data) {
} }
socket.join(data.enter); socket.join(data.enter);
server.rooms[data.enter] = server.rooms[data.enter] || {};
if (socket.uid) {
server.rooms[data.enter][socket.id] = socket.uid;
if (data.leave && server.rooms[data.leave] && server.rooms[data.leave][socket.id] && data.enter !== data.leave) {
delete server.rooms[data.leave][socket.id];
if(!Object.keys(server.rooms[data.leave]).length) {
delete server.rooms[data.leave];
}
}
}
if (data.leave) { if (data.leave) {
module.parent.exports.updateRoomBrowsingText(data.leave); module.parent.exports.updateRoomBrowsingText(data.leave);
@ -104,12 +92,12 @@ SocketMeta.rooms.enter = function(socket, data) {
module.parent.exports.updateRoomBrowsingText(data.enter); module.parent.exports.updateRoomBrowsingText(data.enter);
if (data.enter != 'admin') { if (data.enter != 'admin') {
server.in('admin').emit('event:meta.rooms.update', null, server.rooms); server.in('admin').emit('event:meta.rooms.update', null, server.server.sockets.manager.rooms);
} }
}; };
SocketMeta.rooms.getAll = function(socket, data, callback) { SocketMeta.rooms.getAll = function(socket, data, callback) {
callback(null, server.rooms); callback(null, server.server.sockets.manager.rooms);
}; };
/* Exports */ /* Exports */

Loading…
Cancel
Save