mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +01:00
took out userSockets, using io.sockets.clients() now'
This commit is contained in:
@@ -81,19 +81,28 @@ define(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('user.isOnline', function(err, data) {
|
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);
|
startLoading('users:online', 0, true);
|
||||||
socket.emit('user.getOnlineAnonCount', {} , function(err, anonCount) {
|
updateAnonCount();
|
||||||
if(parseInt(anonCount, 10) > 0) {
|
|
||||||
$('#users-container .anon-user').removeClass('hide');
|
|
||||||
$('#online_anon_count').html(anonCount);
|
|
||||||
} else {
|
|
||||||
$('#users-container .anon-user').addClass('hide');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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) {
|
function onUsersLoaded(users, emptyContainer) {
|
||||||
var html = templates.prepare(templates['users'].blocks['users']).parse({
|
var html = templates.prepare(templates['users'].blocks['users']).parse({
|
||||||
users: users
|
users: users
|
||||||
|
|||||||
@@ -54,7 +54,7 @@
|
|||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<button class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" type="button" title="[[topic:posted_by]] {posts.username}">
|
<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>
|
<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="username-field" href="{relative_path}/user/{posts.userslug}" itemprop="author">{posts.username} </span>
|
||||||
<span class="caret"></span>
|
<span class="caret"></span>
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -64,11 +64,12 @@ SocketAdmin.user.createUser = function(socket, user, callback) {
|
|||||||
SocketAdmin.user.banUser = function(socket, theirid) {
|
SocketAdmin.user.banUser = function(socket, theirid) {
|
||||||
admin.user.banUser(socket.uid, theirid, socket, function(isBanned) {
|
admin.user.banUser(socket.uid, theirid, socket, function(isBanned) {
|
||||||
if(isBanned) {
|
if(isBanned) {
|
||||||
if(index.userSockets[theirid]) {
|
var sockets = index.getUserSockets(theirid);
|
||||||
for(var i=0; i<index.userSockets[theirid].length; ++i) {
|
|
||||||
index.userSockets[theirid][i].emit('event:banned');
|
for(var i=0; i<sockets.length; ++i) {
|
||||||
}
|
sockets[i].emit('event:banned');
|
||||||
}
|
}
|
||||||
|
|
||||||
module.parent.exports.logoutUser(theirid);
|
module.parent.exports.logoutUser(theirid);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -27,9 +27,6 @@ var SocketIO = require('socket.io'),
|
|||||||
var io;
|
var io;
|
||||||
|
|
||||||
|
|
||||||
Sockets.userSockets = {};
|
|
||||||
|
|
||||||
|
|
||||||
Sockets.init = function(server) {
|
Sockets.init = function(server) {
|
||||||
|
|
||||||
io = socketioWildcard(SocketIO).listen(server, {
|
io = socketioWildcard(SocketIO).listen(server, {
|
||||||
@@ -63,16 +60,13 @@ Sockets.init = function(server) {
|
|||||||
sessionID = socket.handshake.signedCookies["express.sid"];
|
sessionID = socket.handshake.signedCookies["express.sid"];
|
||||||
db.sessionStore.get(sessionID, function(err, sessionData) {
|
db.sessionStore.get(sessionID, function(err, sessionData) {
|
||||||
if (!err && sessionData && sessionData.passport && sessionData.passport.user) {
|
if (!err && sessionData && sessionData.passport && sessionData.passport.user) {
|
||||||
uid = sessionData.passport.user;
|
uid = parseInt(sessionData.passport.user, 10);
|
||||||
} else {
|
} else {
|
||||||
uid = 0;
|
uid = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.uid = parseInt(uid, 10);
|
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 */
|
/* Need to save some state for the logger & maybe some other modules later on */
|
||||||
socket.state = {
|
socket.state = {
|
||||||
user : {
|
user : {
|
||||||
@@ -106,45 +100,44 @@ Sockets.init = function(server) {
|
|||||||
isAdmin: userData.isAdmin,
|
isAdmin: userData.isAdmin,
|
||||||
uid: uid
|
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() {
|
socket.on('disconnect', function() {
|
||||||
|
|
||||||
var index = (Sockets.userSockets[uid] || []).indexOf(socket);
|
if (uid && !Sockets.getUserSockets(uid).length <= 1) {
|
||||||
if (index !== -1) {
|
db.sortedSetRemove('users:online', uid, function(err) {
|
||||||
Sockets.userSockets[uid].splice(index, 1);
|
socketUser.isOnline(socket, uid, function(err, data) {
|
||||||
}
|
socket.broadcast.emit('user.isOnline', err, data);
|
||||||
|
|
||||||
if (Sockets.userSockets[uid] && Sockets.userSockets[uid].length === 0) {
|
|
||||||
delete Sockets.userSockets[uid];
|
|
||||||
|
|
||||||
if (uid) {
|
|
||||||
db.sortedSetRemove('users:online', uid, function(err, data) {
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
socketUser.isOnline(socket, uid, function(err, data) {
|
if (!uid) {
|
||||||
socket.broadcast.emit('user.isOnline', err, data);
|
socket.broadcast.emit('user.anonDisconnect');
|
||||||
});
|
}
|
||||||
|
|
||||||
emitOnlineUserCount();
|
emitOnlineUserCount();
|
||||||
|
|
||||||
for(var roomName in io.sockets.manager.roomClients[socket.id]) {
|
for(var roomName in io.sockets.manager.roomClients[socket.id]) {
|
||||||
updateRoomBrowsingText(roomName.slice(1));
|
updateRoomBrowsingText(roomName.slice(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('*', function(payload, callback) {
|
socket.on('*', function(payload, callback) {
|
||||||
|
|
||||||
function callMethod(method) {
|
function callMethod(method) {
|
||||||
if(socket.uid) {
|
if(socket.uid) {
|
||||||
user.setUserField(socket.uid, 'lastonline', Date.now());
|
user.setUserField(socket.uid, 'lastonline', Date.now());
|
||||||
@@ -187,16 +180,10 @@ Sockets.init = function(server) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Sockets.logoutUser = function(uid) {
|
Sockets.logoutUser = function(uid) {
|
||||||
if(Sockets.userSockets[uid] && Sockets.userSockets[uid].length) {
|
Sockets.getUserSockets(uid).forEach(function(socket) {
|
||||||
for(var i=0; i< Sockets.userSockets[uid].length; ++i) {
|
socket.emit('event:disconnect');
|
||||||
Sockets.userSockets[uid][i].emit('event:disconnect');
|
socket.disconnect();
|
||||||
Sockets.userSockets[uid][i].disconnect();
|
});
|
||||||
|
|
||||||
if(!Sockets.userSockets[uid]) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Sockets.emitUserCount = function() {
|
Sockets.emitUserCount = function() {
|
||||||
@@ -212,18 +199,38 @@ Sockets.in = function(room) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Sockets.getConnectedClients = function() {
|
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 () {
|
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 */
|
/* Helpers */
|
||||||
|
|
||||||
Sockets.isUserOnline = isUserOnline;
|
Sockets.isUserOnline = isUserOnline;
|
||||||
function isUserOnline(uid) {
|
function isUserOnline(uid) {
|
||||||
return !!Sockets.userSockets[uid] && Sockets.userSockets[uid].length > 0;
|
return Sockets.getUserSockets(uid).length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
|
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
|
||||||
@@ -292,11 +299,8 @@ function emitTopicPostStats(callback) {
|
|||||||
|
|
||||||
Sockets.emitOnlineUserCount = emitOnlineUserCount;
|
Sockets.emitOnlineUserCount = emitOnlineUserCount;
|
||||||
function emitOnlineUserCount(callback) {
|
function emitOnlineUserCount(callback) {
|
||||||
var anon = Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0;
|
var anon = Sockets.getOnlineAnonCount(0);
|
||||||
var registered = Object.keys(Sockets.userSockets).length;
|
var registered = Sockets.getConnectedClients().length;
|
||||||
if (anon) {
|
|
||||||
registered = registered - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var returnObj = {
|
var returnObj = {
|
||||||
users: registered + anon,
|
users: registered + anon,
|
||||||
|
|||||||
@@ -113,39 +113,29 @@ SocketModules.chats.send = function(socket, data) {
|
|||||||
usersData[0].uid = socket.uid;
|
usersData[0].uid = socket.uid;
|
||||||
usersData[1].uid = touid;
|
usersData[1].uid = touid;
|
||||||
|
|
||||||
Messaging.parse(msg, socket.uid, socket.uid, usersData[1], usersData[0], true, function(parsed) {
|
Messaging.parse(msg, socket.uid, socket.uid, usersData[1], usersData[0], true, function(parsed) {
|
||||||
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
|
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.getUserSockets(touid).forEach(function(s) {
|
||||||
server.userSockets[touid][x].emit('event:chats.receive', {
|
s.emit('event:chats.receive', {
|
||||||
fromuid: socket.uid,
|
fromuid: socket.uid,
|
||||||
username: username,
|
username: username,
|
||||||
message: parsed,
|
message: parsed,
|
||||||
timestamp: Date.now()
|
timestamp: Date.now()
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (server.userSockets[socket.uid]) {
|
server.getUserSockets(socket.uid).forEach(function(s) {
|
||||||
|
s.emit('event:chats.receive', {
|
||||||
numSockets = server.userSockets[socket.uid].length;
|
fromuid: touid,
|
||||||
|
username: toUsername,
|
||||||
for (x = 0; x < numSockets; ++x) {
|
message: parsed,
|
||||||
server.userSockets[socket.uid][x].emit('event:chats.receive', {
|
timestamp: Date.now()
|
||||||
fromuid: touid,
|
});
|
||||||
username: toUsername,
|
});
|
||||||
message: parsed,
|
});
|
||||||
timestamp: Date.now()
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -647,8 +647,7 @@ var async = require('async'),
|
|||||||
var websockets = require('./socket.io');
|
var websockets = require('./socket.io');
|
||||||
|
|
||||||
if (!uids) {
|
if (!uids) {
|
||||||
clients = websockets.getConnectedClients();
|
uids = websockets.getConnectedClients();
|
||||||
uids = Object.keys(clients);
|
|
||||||
} else if (!Array.isArray(uids)) {
|
} else if (!Array.isArray(uids)) {
|
||||||
uids = [uids];
|
uids = [uids];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user