update all socket methods, works on single node

v1.18.x
barisusakli 10 years ago
parent 13609edf82
commit f7c70e2a7a

@ -314,7 +314,6 @@ var db = require('./database'),
}; };
function sendNotifications(fromuid, touid, messageObj, callback) { function sendNotifications(fromuid, touid, messageObj, callback) {
// todo #1798 -- this should check if the user is in room `chat_{uidA}_{uidB}` instead, see `Sockets.uidInRoom(uid, room);`
if (!websockets.isUserOnline(touid)) { if (!websockets.isUserOnline(touid)) {
notifications.create({ notifications.create({
bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',

@ -130,7 +130,7 @@ Sockets.init = function(server) {
socket.on('disconnect', function() { socket.on('disconnect', function() {
var socketCount = Sockets.getUserSocketCount(socket.uid); var socketCount = Sockets.getUserSocketCount(socket.uid);
console.log('DISCONNECT', socket.uid, socket.id); console.log('DISCONNECT', socket.uid, socket.id);
if (socket.uid && socketCount <= 1) { if (socket.uid && socketCount <= 0) {
socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'}); socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'});
} }
@ -221,84 +221,70 @@ Sockets.in = function(room) {
return io.sockets.in(room); return io.sockets.in(room);
}; };
Sockets.uidInRoom = function(uid, room) {
return false;
var userSocketIds = io.sockets.manager.rooms['/uid_' + uid];
if (!Array.isArray(userSocketIds) || !userSocketIds.length) {
return false;
}
var roomSocketIds = io.sockets.manager.rooms['/' + room];
if (!Array.isArray(roomSocketIds) || !roomSocketIds.length) {
return false;
}
for (var i=0; i<userSocketIds.length; ++i) {
if (roomSocketIds.indexOf(userSocketIds[i]) !== -1) {
return true;
}
}
return false;
};
Sockets.getSocketCount = function() { Sockets.getSocketCount = function() {
return 0; // TODO: io.sockets.adapter.sids is local to this worker
// use redis-adapter
var clients = io.sockets.manager.rooms['']; var clients = Object.keys(io.sockets.adapter.sids || {});
return Array.isArray(clients) ? clients.length : 0; return Array.isArray(clients) ? clients.length : 0;
}; };
Sockets.getUserSocketCount = function(uid) { Sockets.getUserSocketCount = function(uid) {
return 0; // TODO: io.sockets.adapter.sids is local to this worker
// use .clients('uid_' + uid, fn)
var roomClients = io.sockets.manager.rooms['/uid_' + uid]; var roomClients = Object.keys(io.sockets.adapter.rooms['uid_' + uid] || {});
if(!Array.isArray(roomClients)) { return Array.isArray(roomClients) ? roomClients.length : 0;
return 0;
}
return roomClients.length;
}; };
Sockets.getOnlineAnonCount = function () { Sockets.getOnlineAnonCount = function () {
return 0; // TODO: io.sockets.adapter.rooms is local to this worker
// use .clients()
var guestRoom = io.sockets.manager.rooms['/online_guests']; var guestSocketIds = Object.keys(io.sockets.adapter.rooms.online_guests || {});
if (!Array.isArray(guestRoom)) { return Array.isArray(guestSocketIds) ? guestSocketIds.length : 0;
return 0;
}
return guestRoom.length;
}; };
Sockets.getUserSockets = function(uid) { Sockets.getUserSockets = function(uid) {
return []; // TODO: doesn't work in cluster
var sockets = io.sockets.clients(); var userSocketIds = Object.keys(io.sockets.adapter.rooms['uid_' + uid] || {});
if(!sockets || !sockets.length) { if (!Array.isArray(userSocketIds) || !userSocketIds.length) {
return []; return [];
} }
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
sockets = sockets.filter(function(s) { var sockets = [];
return s.uid === uid; userSocketIds.forEach(function(sid) {
if (io.sockets.connected[sid] && io.sockets.connected[sid].uid === uid) {
sockets.push(io.sockets.connected[sid]) ;
}
}); });
return sockets; return sockets;
}; };
Sockets.getUserRooms = function(uid) { Sockets.getUserRooms = function(uid) {
return {}; // TODO:
// io.sockets.adapter.rooms is local to this worker
// io.sockets.adapter.sids is local to this worker
// use .clients('uid_' + uid, fn)
// user .roomClients(socketId, fn)
var rooms = {}; var rooms = {};
var uidSocketIds = io.sockets.manager.rooms['/uid_' + uid];
if (!io.sockets.adapter.rooms['uid_' + uid]) {
return [];
}
var uidSocketIds = Object.keys(io.sockets.adapter.rooms['uid_' + uid]);
if (!Array.isArray(uidSocketIds)) { if (!Array.isArray(uidSocketIds)) {
return []; return [];
} }
for (var i=0; i<uidSocketIds.length; ++i) { for (var i=0; i<uidSocketIds.length; ++i) {
var roomClients = io.sockets.manager.roomClients[uidSocketIds[i]]; var roomClients = io.sockets.adapter.sids[uidSocketIds[i]];
for (var roomName in roomClients) { for (var roomName in roomClients) {
if (roomName && roomClients.hasOwnProperty(roomName)) { if (roomName && roomClients.hasOwnProperty(roomName)) {
rooms[roomName.slice(1)] = true; rooms[roomName] = true;
} }
} }
} }
@ -324,19 +310,13 @@ Sockets.reqFromSocket = function(socket) {
}; };
Sockets.isUserOnline = function(uid) { Sockets.isUserOnline = function(uid) {
return false; // TODO: io.sockets.adapter.rooms is local to this worker
if (!io) { // use .clients('uid_' + uid, fn)
// Special handling for install script (socket.io not initialised) return io ? !!io.sockets.adapter.rooms['uid_' + uid] : false;
return false;
}
return Array.isArray(io.sockets.manager.rooms['/uid_' + uid]);
}; };
Sockets.isUsersOnline = function(uids, callback) { Sockets.isUsersOnline = function(uids, callback) {
var data = uids.map(Sockets.isUserOnline); callback(null, uids.map(Sockets.isUserOnline));
callback(null, data);
}; };
Sockets.updateRoomBrowsingText = function (roomName, selfUid) { Sockets.updateRoomBrowsingText = function (roomName, selfUid) {
@ -371,19 +351,20 @@ Sockets.updateRoomBrowsingText = function (roomName, selfUid) {
}; };
Sockets.getUidsInRoom = function(roomName) { Sockets.getUidsInRoom = function(roomName) {
return []; // TODO : doesnt work in cluster
var uids = []; var uids = [];
roomName = roomName ? '/' + roomName : '';
var socketids = io.sockets.manager.rooms[roomName]; var socketids = Object.keys(io.sockets.adapter.rooms[roomName] || {});
if (!Array.isArray(socketids)) { if (!Array.isArray(socketids) || !socketids.length) {
return []; return [];
} }
for(var i=0; i<socketids.length; ++i) { for(var i=0; i<socketids.length; ++i) {
var socketRooms = Object.keys(io.sockets.manager.roomClients[socketids[i]]); var socketRooms = Object.keys(io.sockets.adapter.sids[socketids[i]]);
if (Array.isArray(socketRooms)) { if (Array.isArray(socketRooms)) {
socketRooms.forEach(function(roomName) { socketRooms.forEach(function(roomName) {
if (roomName.indexOf('/uid_') === 0 ) { if (roomName.indexOf('uid_') === 0 ) {
uids.push(roomName.split('_')[1]); uids.push(roomName.split('_')[1]);
} }
}); });

Loading…
Cancel
Save