mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-24 01:10:31 +01:00
took out userSockets, using io.sockets.clients() now'
This commit is contained in:
@@ -81,9 +81,19 @@ 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);
|
||||
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);
|
||||
@@ -92,7 +102,6 @@ define(function() {
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function onUsersLoaded(users, emptyContainer) {
|
||||
var html = templates.prepare(templates['users'].blocks['users']).parse({
|
||||
|
||||
@@ -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 /> </span>
|
||||
<span class="visible-xs-inline visible-md-inline"><img class="" src="{posts.picture}" width=18 height=18 /> </span>
|
||||
<span class="username-field" href="{relative_path}/user/{posts.userslug}" itemprop="author">{posts.username} </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');
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
@@ -115,35 +115,25 @@ SocketModules.chats.send = function(socket, data) {
|
||||
|
||||
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', {
|
||||
server.getUserSockets(touid).forEach(function(s) {
|
||||
s.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', {
|
||||
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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user