mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-21 07:50:37 +01:00
moving onlineUsers from an in-memory variable to a sorted set
This commit is contained in:
@@ -133,8 +133,8 @@ Loader.addClusterEvents = function(callback) {
|
|||||||
Loader.primaryWorker = parseInt(worker.id, 10);
|
Loader.primaryWorker = parseInt(worker.id, 10);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'user:connect':
|
// case 'user:connect':
|
||||||
case 'user:disconnect':
|
// case 'user:disconnect':
|
||||||
case 'config:update':
|
case 'config:update':
|
||||||
Loader.notifyWorkers(message);
|
Loader.notifyWorkers(message);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -26,43 +26,68 @@ var io;
|
|||||||
|
|
||||||
var onlineUsers = [];
|
var onlineUsers = [];
|
||||||
|
|
||||||
process.on('message', onMessage);
|
// process.on('message', onMessage);
|
||||||
|
|
||||||
function onMessage(msg) {
|
// function onMessage(msg) {
|
||||||
if (typeof msg !== 'object') {
|
// if (typeof msg !== 'object') {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (msg.action === 'user:connect') {
|
// if (msg.action === 'user:connect') {
|
||||||
if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) {
|
// if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) {
|
||||||
onlineUsers.push(msg.uid);
|
// onlineUsers.push(msg.uid);
|
||||||
}
|
// }
|
||||||
} else if(msg.action === 'user:disconnect') {
|
// } else if(msg.action === 'user:disconnect') {
|
||||||
if (msg.uid && msg.socketCount <= 1) {
|
// if (msg.uid && msg.socketCount <= 1) {
|
||||||
var index = onlineUsers.indexOf(msg.uid);
|
// var index = onlineUsers.indexOf(msg.uid);
|
||||||
if (index !== -1) {
|
// if (index !== -1) {
|
||||||
onlineUsers.splice(index, 1);
|
// onlineUsers.splice(index, 1);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
function onUserConnect(uid, socketid) {
|
function onUserConnect(uid, socketid) {
|
||||||
var msg = {action: 'user:connect', uid: uid, socketid: socketid};
|
db.sortedSetIncrBy('onlineUsers', 1, uid, function(err, score) {
|
||||||
if (process.send) {
|
if (err) {
|
||||||
process.send(msg);
|
return winston.error('[socket.io] Could not add socket id ' + socketid + ' (uid ' + uid + ') to the online users list.');
|
||||||
} 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) {
|
function onUserDisconnect(uid, socketid, socketCount) {
|
||||||
var msg = {action: 'user:disconnect', uid: uid, socketid: socketid, socketCount: socketCount};
|
// baris, I no longer use socketCount here, since the zset score is the # of connections, just FYI.
|
||||||
if (process.send) {
|
db.sortedSetIncrBy('onlineUsers', -1, uid, function(err, score) {
|
||||||
process.send(msg);
|
if (err) {
|
||||||
} else {
|
return winston.error('[socket.io] Could not remove socket id ' + socketid + ' (uid ' + uid + ') from the online users list.');
|
||||||
onMessage(msg);
|
}
|
||||||
}
|
|
||||||
|
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) {
|
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) {
|
io.sockets.on('connection', function(socket) {
|
||||||
var hs = socket.handshake,
|
var hs = socket.handshake,
|
||||||
sessionID, uid;
|
sessionID, uid;
|
||||||
|
|||||||
Reference in New Issue
Block a user