possible fix to online guest count

using socket io rooms instead of syncing between workers.
v1.18.x
barisusakli 11 years ago
parent e79c980421
commit 20eb4def1d

@ -24,9 +24,7 @@ var SocketIO = require('socket.io'),
var io; var io;
var onlineUsersMap = {};
var onlineUsers = []; var onlineUsers = [];
var uidToSocketId = {};
var socketIdToUid = {}; var socketIdToUid = {};
process.on('message', onMessage); process.on('message', onMessage);
@ -37,41 +35,19 @@ function onMessage(msg) {
} }
if (msg.action === 'user:connect') { 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) { if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) {
onlineUsers.push(msg.uid); onlineUsers.push(msg.uid);
} }
if (Array.isArray(uidToSocketId[msg.uid])) {
uidToSocketId[msg.uid].push(msg.socketid);
} else {
uidToSocketId[msg.uid] = [msg.socketid];
}
socketIdToUid[msg.socketid] = msg.uid; socketIdToUid[msg.socketid] = msg.uid;
} else if(msg.action === 'user:disconnect') { } else if(msg.action === 'user:disconnect') {
if (onlineUsersMap[msg.uid]) { if (msg.uid && msg.socketCount <= 1) {
onlineUsersMap[msg.uid] -= 1; var index = onlineUsers.indexOf(msg.uid);
onlineUsersMap[msg.uid] = Math.max(0, onlineUsersMap[msg.uid]);
}
var index = 0;
if (msg.uid && onlineUsersMap[msg.uid] === 0) {
index = onlineUsers.indexOf(msg.uid);
if (index !== -1) { if (index !== -1) {
onlineUsers.splice(index, 1); onlineUsers.splice(index, 1);
} }
} }
if (uidToSocketId[msg.uid]) {
index = uidToSocketId[msg.uid].indexOf(msg.socketid);
if (index !== -1) {
uidToSocketId[msg.uid].splice(index, 1);
}
}
delete socketIdToUid[msg.socketid]; delete socketIdToUid[msg.socketid];
} }
} }
@ -85,8 +61,8 @@ function onUserConnect(uid, socketid) {
} }
} }
function onUserDisconnect(uid, socketid) { function onUserDisconnect(uid, socketid, socketCount) {
var msg = {action: 'user:disconnect', uid: uid, socketid: socketid}; var msg = {action: 'user:disconnect', uid: uid, socketid: socketid, socketCount: socketCount};
if (process.send) { if (process.send) {
process.send(msg); process.send(msg);
} else { } else {
@ -155,10 +131,9 @@ Sockets.init = function(server) {
logger.io_one(socket, uid); logger.io_one(socket, uid);
if (uid) { if (uid) {
db.sortedSetAdd('users:online', Date.now(), uid, function(err) {
socket.join('uid_' + uid); socket.join('uid_' + uid);
socket.join('online_users');
db.sortedSetAdd('users:online', Date.now(), uid, function(err) {
async.parallel({ async.parallel({
user: function(next) { user: function(next) {
user.getUserFields(uid, ['username', 'userslug', 'picture', 'status'], next); user.getUserFields(uid, ['username', 'userslug', 'picture', 'status'], next);
@ -183,6 +158,7 @@ Sockets.init = function(server) {
}); });
}); });
} else { } else {
socket.join('online_guests');
socket.emit('event:connect', { socket.emit('event:connect', {
status: 1, status: 1,
username: '[[global:guest]]', username: '[[global:guest]]',
@ -194,8 +170,8 @@ Sockets.init = function(server) {
}); });
socket.on('disconnect', function() { socket.on('disconnect', function() {
var socketCount = Sockets.getUserSocketCount(uid);
if (uid && (!onlineUsersMap[uid] || onlineUsersMap[uid] <= 1)) { if (uid && socketCount <= 1) {
db.sortedSetRemove('users:online', uid, function(err) { db.sortedSetRemove('users:online', uid, function(err) {
if (err) { if (err) {
return winston.error(err.message); return winston.error(err.message);
@ -204,7 +180,7 @@ Sockets.init = function(server) {
}); });
} }
onUserDisconnect(uid, socket.id); onUserDisconnect(uid, socket.id, socketCount);
emitOnlineUserCount(); emitOnlineUserCount();
@ -292,16 +268,42 @@ Sockets.uidInRoom = function(uid, room) {
}; };
Sockets.getSocketCount = function() { Sockets.getSocketCount = function() {
return Object.keys(socketIdToUid).length; var clients = io.sockets.manager.rooms[''];
if (!Array.isArray(clients)) {
return 0;
}
return clients.length;
}; };
Sockets.getConnectedClients = function() { Sockets.getConnectedClients = function() {
return onlineUsers; return onlineUsers;
}; };
Sockets.getUserSocketCount = function(uid) {
var roomClients = io.sockets.manager.rooms['/uid_' + uid];
if(!Array.isArray(roomClients)) {
return 0;
}
return roomClients.length;
};
Sockets.getOnlineUserCount = function () {
var roomNames = Object.keys(io.sockets.manager.rooms);
if (!Array.isArray(roomNames)) {
return 0;
}
roomNames = roomNames.filter(function(name) {
return name.indexOf('/uid_') === 0;
});
return roomNames.length;
};
Sockets.getOnlineAnonCount = function () { Sockets.getOnlineAnonCount = function () {
var count = parseInt(onlineUsersMap[0], 10); var guestRoom = io.sockets.manager.rooms['/online_guests'];
return count ? count : 0; if (!Array.isArray(guestRoom)) {
return 0;
}
return guestRoom.length;
}; };
Sockets.getUserSockets = function(uid) { Sockets.getUserSockets = function(uid) {
@ -321,7 +323,7 @@ Sockets.getUserSockets = function(uid) {
Sockets.getUserRooms = function(uid) { Sockets.getUserRooms = function(uid) {
var rooms = {}; var rooms = {};
var uidSocketIds = uidToSocketId[uid]; var uidSocketIds = io.sockets.manager.rooms['/uid_' + uid];
if (!Array.isArray(uidSocketIds)) { if (!Array.isArray(uidSocketIds)) {
return []; return [];
} }
@ -359,13 +361,11 @@ Sockets.reqFromSocket = function(socket) {
Sockets.isUserOnline = isUserOnline; Sockets.isUserOnline = isUserOnline;
function isUserOnline(uid) { function isUserOnline(uid) {
return !!onlineUsersMap[uid]; return Array.isArray(io.sockets.manager.rooms['/uid_' + uid]);
} }
Sockets.isUsersOnline = function(uids, callback) { Sockets.isUsersOnline = function(uids, callback) {
var data = uids.map(function(uid) { var data = uids.map(isUserOnline);
return !!onlineUsersMap[uid];
});
callback(null, data); callback(null, data);
}; };
@ -457,7 +457,7 @@ function emitTopicPostStats(callback) {
Sockets.emitOnlineUserCount = emitOnlineUserCount; Sockets.emitOnlineUserCount = emitOnlineUserCount;
function emitOnlineUserCount(callback) { function emitOnlineUserCount(callback) {
var anon = Sockets.getOnlineAnonCount(); var anon = Sockets.getOnlineAnonCount();
var registered = Sockets.getConnectedClients().length; var registered = Sockets.getOnlineUserCount();
var returnObj = { var returnObj = {
users: registered + anon, users: registered + anon,

@ -85,7 +85,7 @@ SocketMeta.rooms.enter = function(socket, data, callback) {
SocketMeta.rooms.getAll = function(socket, data, callback) { SocketMeta.rooms.getAll = function(socket, data, callback) {
var userData = { var userData = {
onlineGuestCount: websockets.getOnlineAnonCount(), onlineGuestCount: websockets.getOnlineAnonCount(),
onlineRegisteredCount: websockets.getConnectedClients().length, onlineRegisteredCount: websockets.getOnlineUserCount(),
socketCount: websockets.getSocketCount() socketCount: websockets.getSocketCount()
}; };

Loading…
Cancel
Save