online count fixes for cluster

v1.18.x
barisusakli 11 years ago
parent 8a996c4524
commit 98f338b41c

@ -125,11 +125,21 @@ Loader.init = function() {
console.log('[cluster] Restarting...'); console.log('[cluster] Restarting...');
}); });
break; break;
case 'user:connect':
case 'user:disconnect':
notifyWorkers(worker, message);
break;
} }
} }
}); });
}); });
function notifyWorkers(currentWorker, msg) {
Object.keys(cluster.workers).forEach(function(id) {
cluster.workers[id].send(msg);
});
}
cluster.on('exit', function(worker, code, signal) { cluster.on('exit', function(worker, code, signal) {
if (code !== 0) { if (code !== 0) {
if (Loader.timesStarted < numCPUs*3) { if (Loader.timesStarted < numCPUs*3) {
@ -159,7 +169,7 @@ Loader.restart = function(callback) {
cluster.workers[id].kill(); cluster.workers[id].kill();
next(); next();
}, callback); }, callback);
} };
Loader.init(); Loader.init();

@ -94,10 +94,12 @@ define('forum/admin/index', function() {
Admin.updateRoomUsage = function(err, data) { Admin.updateRoomUsage = function(err, data) {
var roomData = data.rooms;
function getUserCountIn(room) { function getUserCountIn(room) {
var count = 0; var count = 0;
for(var user in data[room]) { for(var user in roomData[room]) {
if (data[room].hasOwnProperty(user)) { if (roomData[room].hasOwnProperty(user)) {
++count; ++count;
} }
} }
@ -114,10 +116,10 @@ define('forum/admin/index', function() {
var sortedData = []; var sortedData = [];
for (var room in data) { for (var room in roomData) {
if (room !== '') { if (room !== '') {
sortedData.push({room: room, count: data[room].length}); sortedData.push({room: room, count: roomData[room].length});
total += data[room].length; total += roomData[room].length;
} }
} }
@ -131,6 +133,12 @@ define('forum/admin/index', function() {
sortedData[i].count + " active user" + (sortedData[i].count > 1 ? "s" : "") + "</div>"; sortedData[i].count + " active user" + (sortedData[i].count > 1 ? "s" : "") + "</div>";
} }
var parent = active_users.parent();
parent.prepend('<hr/>');
parent.prepend('<strong>Online Total [ ' + (data.onlineRegisteredCount + data.onlineGuestCount) + ' ]</strong>');
parent.prepend('<strong>Online Guests [ ' + data.onlineGuestCount + ' ]</strong><br/>');
parent.prepend('<strong>Online Users [ ' + data.onlineRegisteredCount + ' ]</strong><br/>');
active_users.html(usersHtml); active_users.html(usersHtml);
$('#connections').html(total); $('#connections').html(total);
}; };

@ -76,11 +76,7 @@ User.banUser = function(uid, callback) {
return callback(err); return callback(err);
} }
var sockets = websockets.getUserSockets(uid); websockets.in('uid_' + uid).emit('event:banned');
for(var i=0; i<sockets.length; ++i) {
sockets[i].emit('event:banned');
}
websockets.logoutUser(uid); websockets.logoutUser(uid);
callback(); callback();

@ -25,6 +25,36 @@ var SocketIO = require('socket.io'),
var io; var io;
var onlineUsersMap = {};
var onlineUsers = [];
process.on('message', function(msg) {
if (typeof msg !== 'object') {
return;
}
if (msg.action === 'user:connect') {
if (!onlineUsersMap[msg.uid]) {
onlineUsersMap[msg.uid] = 1;
} else {
onlineUsersMap[msg.uid]++;
}
if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) {
onlineUsers.push(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]);
}
}
});
Sockets.init = function(server) { Sockets.init = function(server) {
var RedisStore = require('socket.io/lib/stores/redis'), var RedisStore = require('socket.io/lib/stores/redis'),
@ -47,6 +77,8 @@ Sockets.init = function(server) {
Sockets.server = io; Sockets.server = io;
db.delete('users:online');
fs.readdir(__dirname, function(err, files) { fs.readdir(__dirname, function(err, files) {
files.splice(files.indexOf('index.js'), 1); files.splice(files.indexOf('index.js'), 1);
@ -79,7 +111,9 @@ Sockets.init = function(server) {
} }
socket.uid = parseInt(uid, 10); socket.uid = parseInt(uid, 10);
if (process.send) {
process.send({action: 'user:connect', uid: uid});
}
/* If meta.config.loggerIOStatus > 0, logger.io_one will hook into this socket */ /* If meta.config.loggerIOStatus > 0, logger.io_one will hook into this socket */
logger.io_one(socket, uid); logger.io_one(socket, uid);
@ -122,7 +156,7 @@ Sockets.init = function(server) {
socket.on('disconnect', function() { socket.on('disconnect', function() {
if (uid && Sockets.getUserSockets(uid).length <= 1) { if (uid && (!onlineUsersMap[uid] || onlineUsersMap[uid] <= 1)) {
db.sortedSetRemove('users:online', uid, function(err) { db.sortedSetRemove('users:online', uid, function(err) {
socketUser.isOnline(socket, uid, function(err, data) { socketUser.isOnline(socket, uid, function(err, data) {
socket.broadcast.emit('user.isOnline', err, data); socket.broadcast.emit('user.isOnline', err, data);
@ -130,6 +164,10 @@ Sockets.init = function(server) {
}); });
} }
if (process.send) {
process.send({action: 'user:disconnect', uid: uid});
}
emitOnlineUserCount(); emitOnlineUserCount();
for(var roomName in io.sockets.manager.roomClients[socket.id]) { for(var roomName in io.sockets.manager.roomClients[socket.id]) {
@ -214,22 +252,12 @@ Sockets.uidInRoom = function(uid, room) {
}; };
Sockets.getConnectedClients = function() { Sockets.getConnectedClients = function() {
var uids = []; return onlineUsers;
if (!io) {
return uids;
}
var clients = io.sockets.clients();
clients.forEach(function(client) {
if(client.uid && uids.indexOf(client.uid) === -1) {
uids.push(client.uid);
}
});
return uids;
}; };
Sockets.getOnlineAnonCount = function () { Sockets.getOnlineAnonCount = function () {
return Sockets.getUserSockets(0).length; var count = parseInt(onlineUsersMap[0], 10);
return count ? count : 0;
}; };
Sockets.getUserSockets = function(uid) { Sockets.getUserSockets = function(uid) {
@ -281,22 +309,12 @@ Sockets.reqFromSocket = function(socket) {
Sockets.isUserOnline = isUserOnline; Sockets.isUserOnline = isUserOnline;
function isUserOnline(uid) { function isUserOnline(uid) {
return Sockets.getUserSockets(uid).length > 0; return !!onlineUsersMap[uid];
} }
Sockets.isUsersOnline = function(uids, callback) { Sockets.isUsersOnline = function(uids, callback) {
var sockets = io.sockets.clients();
if(!Array.isArray(sockets) || !sockets.length) {
return callback(null, []);
}
sockets = sockets.map(function(s) {
return s.uid;
});
var data = uids.map(function(uid) { var data = uids.map(function(uid) {
return sockets.indexOf(parseInt(uid, 10)) !== -1; return !!onlineUsersMap[uid];
}); });
callback(null, data); callback(null, data);
@ -373,7 +391,7 @@ function emitTopicPostStats(callback) {
Sockets.emitOnlineUserCount = emitOnlineUserCount; Sockets.emitOnlineUserCount = emitOnlineUserCount;
function emitOnlineUserCount(callback) { function emitOnlineUserCount(callback) {
var anon = Sockets.getOnlineAnonCount(0); var anon = Sockets.getOnlineAnonCount();
var registered = Sockets.getConnectedClients().length; var registered = Sockets.getConnectedClients().length;
var returnObj = { var returnObj = {

@ -78,13 +78,19 @@ SocketMeta.rooms.enter = function(socket, data, callback) {
module.parent.exports.updateRoomBrowsingText(data.enter); module.parent.exports.updateRoomBrowsingText(data.enter);
if (data.enter !== 'admin') { //if (data.enter !== 'admin') {
websockets.in('admin').emit('event:meta.rooms.update', null, websockets.server.sockets.manager.rooms); // websockets.in('admin').emit('event:meta.rooms.update', null, websockets.server.sockets.manager.rooms);
} //}
}; };
SocketMeta.rooms.getAll = function(socket, data, callback) { SocketMeta.rooms.getAll = function(socket, data, callback) {
callback(null, websockets.server.sockets.manager.rooms); var userData = {
onlineGuestCount: websockets.getOnlineAnonCount(),
onlineRegisteredCount: websockets.getConnectedClients().length,
rooms: websockets.server.sockets.manager.rooms
}
callback(null, userData);
}; };
/* Exports */ /* Exports */

@ -181,11 +181,8 @@ SocketModules.chats.send = function(socket, data, callback) {
Messaging.verifySpammer(socket.uid, function(err, isSpammer) { Messaging.verifySpammer(socket.uid, function(err, isSpammer) {
if (!err && isSpammer) { if (!err && isSpammer) {
var sockets = server.getUserSockets(socket.uid);
for(var i = 0; i < sockets.length; ++i) { server.in('uid_' + socket.uid).emit('event:banned');
sockets[i].emit('event:banned');
}
// We're just logging them out, so a "temporary ban" to prevent abuse. Revisit once we implement a way to temporarily ban users // We're just logging them out, so a "temporary ban" to prevent abuse. Revisit once we implement a way to temporarily ban users
server.logoutUser(socket.uid); server.logoutUser(socket.uid);

Loading…
Cancel
Save