took out userSockets, using io.sockets.clients() now'

v1.18.x
Baris Soner Usakli 11 years ago
parent dc592853fc
commit 56cdc86a37

@ -81,19 +81,28 @@ define(function() {
});
socket.on('user.isOnline', function(err, data) {
if(getActiveSection().indexOf('online') === 0 && !loadingMoreUsers) {
var section = getActiveSection();
if((section.indexOf('online') === 0 || section.indexOf('users') === 0) && !loadingMoreUsers) {
startLoading('users:online', 0, true);
socket.emit('user.getOnlineAnonCount', {} , function(err, anonCount) {
if(parseInt(anonCount, 10) > 0) {
$('#users-container .anon-user').removeClass('hide');
$('#online_anon_count').html(anonCount);
} else {
$('#users-container .anon-user').addClass('hide');
}
});
updateAnonCount();
}
});
socket.on('user.anonDisconnect', updateAnonCount);
socket.on('user.anonConnect', updateAnonCount)
function updateAnonCount() {
socket.emit('user.getOnlineAnonCount', {} , function(err, anonCount) {
if(parseInt(anonCount, 10) > 0) {
$('#users-container .anon-user').removeClass('hide');
$('#online_anon_count').html(anonCount);
} else {
$('#users-container .anon-user').addClass('hide');
}
});
}
function onUsersLoaded(users, emptyContainer) {
var html = templates.prepare(templates['users'].blocks['users']).parse({
users: users

@ -54,7 +54,7 @@
<div class="btn-group">
<button class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" type="button" title="[[topic:posted_by]] {posts.username}">
<i class="fa fa-circle status offline"></i>
<span class="visible-xs visible-sm pull-left"><img class="" src="{posts.picture}" width=18 height=18 />&nbsp;</span>
<span class="visible-xs-inline visible-md-inline"><img class="" src="{posts.picture}" width=18 height=18 />&nbsp;</span>
<span class="username-field" href="{relative_path}/user/{posts.userslug}" itemprop="author">{posts.username}&nbsp;</span>
<span class="caret"></span>
</button>

@ -64,11 +64,12 @@ SocketAdmin.user.createUser = function(socket, user, callback) {
SocketAdmin.user.banUser = function(socket, theirid) {
admin.user.banUser(socket.uid, theirid, socket, function(isBanned) {
if(isBanned) {
if(index.userSockets[theirid]) {
for(var i=0; i<index.userSockets[theirid].length; ++i) {
index.userSockets[theirid][i].emit('event:banned');
}
var sockets = index.getUserSockets(theirid);
for(var i=0; i<sockets.length; ++i) {
sockets[i].emit('event:banned');
}
module.parent.exports.logoutUser(theirid);
}
});

@ -27,9 +27,6 @@ var SocketIO = require('socket.io'),
var io;
Sockets.userSockets = {};
Sockets.init = function(server) {
io = socketioWildcard(SocketIO).listen(server, {
@ -63,16 +60,13 @@ Sockets.init = function(server) {
sessionID = socket.handshake.signedCookies["express.sid"];
db.sessionStore.get(sessionID, function(err, sessionData) {
if (!err && sessionData && sessionData.passport && sessionData.passport.user) {
uid = sessionData.passport.user;
uid = parseInt(sessionData.passport.user, 10);
} else {
uid = 0;
}
socket.uid = parseInt(uid, 10);
Sockets.userSockets[uid] = Sockets.userSockets[uid] || [];
Sockets.userSockets[uid].push(socket);
/* Need to save some state for the logger & maybe some other modules later on */
socket.state = {
user : {
@ -106,45 +100,44 @@ Sockets.init = function(server) {
isAdmin: userData.isAdmin,
uid: uid
});
socketUser.isOnline(socket, uid, function(err, data) {
console.log('deeerp');
socket.broadcast.emit('user.isOnline', err, data);
});
});
});
} else {
socket.broadcast.emit('user.anonConnect');
}
socketUser.isOnline(socket, uid, function(err, data) {
socket.broadcast.emit('user.isOnline', err, data);
});
});
});
socket.on('disconnect', function() {
var index = (Sockets.userSockets[uid] || []).indexOf(socket);
if (index !== -1) {
Sockets.userSockets[uid].splice(index, 1);
}
if (Sockets.userSockets[uid] && Sockets.userSockets[uid].length === 0) {
delete Sockets.userSockets[uid];
if (uid) {
db.sortedSetRemove('users:online', uid, function(err, data) {
if (uid && !Sockets.getUserSockets(uid).length <= 1) {
db.sortedSetRemove('users:online', uid, function(err) {
socketUser.isOnline(socket, uid, function(err, data) {
socket.broadcast.emit('user.isOnline', err, data);
});
}
});
}
socketUser.isOnline(socket, uid, function(err, data) {
socket.broadcast.emit('user.isOnline', err, data);
});
if (!uid) {
socket.broadcast.emit('user.anonDisconnect');
}
emitOnlineUserCount();
for(var roomName in io.sockets.manager.roomClients[socket.id]) {
updateRoomBrowsingText(roomName.slice(1));
}
});
socket.on('*', function(payload, callback) {
function callMethod(method) {
if(socket.uid) {
user.setUserField(socket.uid, 'lastonline', Date.now());
@ -187,16 +180,10 @@ Sockets.init = function(server) {
};
Sockets.logoutUser = function(uid) {
if(Sockets.userSockets[uid] && Sockets.userSockets[uid].length) {
for(var i=0; i< Sockets.userSockets[uid].length; ++i) {
Sockets.userSockets[uid][i].emit('event:disconnect');
Sockets.userSockets[uid][i].disconnect();
if(!Sockets.userSockets[uid]) {
return;
}
}
}
Sockets.getUserSockets(uid).forEach(function(socket) {
socket.emit('event:disconnect');
socket.disconnect();
});
};
Sockets.emitUserCount = function() {
@ -212,18 +199,38 @@ Sockets.in = function(room) {
};
Sockets.getConnectedClients = function() {
return Sockets.userSockets;
var clients = io.sockets.clients();
var uids = [];
clients.forEach(function(client) {
if(client.uid && uids.indexOf(client.uid) === -1) {
uids.push(client.uid);
}
});
return uids;
};
Sockets.getOnlineAnonCount = function () {
return Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0;
return Sockets.getUserSockets(0).length;
};
Sockets.getUserSockets = function(uid) {
var sockets = io.sockets.clients();
if(!sockets || !sockets.length) {
return [];
}
sockets = sockets.filter(function(s) {
return s.uid === parseInt(uid, 10);
});
return sockets;
};
/* Helpers */
Sockets.isUserOnline = isUserOnline;
function isUserOnline(uid) {
return !!Sockets.userSockets[uid] && Sockets.userSockets[uid].length > 0;
return Sockets.getUserSockets(uid).length > 0;
}
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
@ -292,11 +299,8 @@ function emitTopicPostStats(callback) {
Sockets.emitOnlineUserCount = emitOnlineUserCount;
function emitOnlineUserCount(callback) {
var anon = Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0;
var registered = Object.keys(Sockets.userSockets).length;
if (anon) {
registered = registered - 1;
}
var anon = Sockets.getOnlineAnonCount(0);
var registered = Sockets.getConnectedClients().length;
var returnObj = {
users: registered + anon,

@ -113,39 +113,29 @@ SocketModules.chats.send = function(socket, data) {
usersData[0].uid = socket.uid;
usersData[1].uid = touid;
Messaging.parse(msg, socket.uid, socket.uid, usersData[1], usersData[0], true, function(parsed) {
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
var numSockets = 0,
x;
if (server.userSockets[touid]) {
numSockets = server.userSockets[touid].length;
for (x = 0; x < numSockets; ++x) {
server.userSockets[touid][x].emit('event:chats.receive', {
fromuid: socket.uid,
username: username,
message: parsed,
timestamp: Date.now()
});
}
}
if (server.userSockets[socket.uid]) {
numSockets = server.userSockets[socket.uid].length;
for (x = 0; x < numSockets; ++x) {
server.userSockets[socket.uid][x].emit('event:chats.receive', {
fromuid: touid,
username: toUsername,
message: parsed,
timestamp: Date.now()
});
}
}
});
});
Messaging.parse(msg, socket.uid, socket.uid, usersData[1], usersData[0], true, function(parsed) {
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
server.getUserSockets(touid).forEach(function(s) {
s.emit('event:chats.receive', {
fromuid: socket.uid,
username: username,
message: parsed,
timestamp: Date.now()
});
});
server.getUserSockets(socket.uid).forEach(function(s) {
s.emit('event:chats.receive', {
fromuid: touid,
username: toUsername,
message: parsed,
timestamp: Date.now()
});
});
});
});
});
};

@ -647,8 +647,7 @@ var async = require('async'),
var websockets = require('./socket.io');
if (!uids) {
clients = websockets.getConnectedClients();
uids = Object.keys(clients);
uids = websockets.getConnectedClients();
} else if (!Array.isArray(uids)) {
uids = [uids];
}

Loading…
Cancel
Save