Node.js based forum software built for the modern web
took out userSockets, using io.sockets.clients() now'
Baris Soner Usakli56cdc86a378a
diff --git a/public/src/forum/users.js b/public/src/forum/users.jsindex f4e9ff988d..3021ad36dc 100644--- a/public/src/forum/users.js+++ b/public/src/forum/users.js@@ -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: usersdiff --git a/public/templates/topic.tpl b/public/templates/topic.tplindex 1643fc76f1..f47a76403a 100644--- a/public/templates/topic.tpl+++ b/public/templates/topic.tpl@@ -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>diff --git a/src/socket.io/admin.js b/src/socket.io/admin.jsindex b4f292af19..b2f6b73742 100644--- a/src/socket.io/admin.js+++ b/src/socket.io/admin.js@@ -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);}});diff --git a/src/socket.io/index.js b/src/socket.io/index.jsindex 9e5b6205af..b4156ee069 100644--- a/src/socket.io/index.js+++ b/src/socket.io/index.js@@ -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,diff --git a/src/socket.io/modules.js b/src/socket.io/modules.jsindex 4a89b32ff4..4d7e8f53d5 100644--- a/src/socket.io/modules.js+++ b/src/socket.io/modules.js@@ -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()+ });+ });+ });+ });});};diff --git a/src/topics.js b/src/topics.jsindex 653ffb2fc3..479c4401a4 100644--- a/src/topics.js+++ b/src/topics.js@@ -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];}