changes to online users code

v1.18.x
Baris Usakli 12 years ago
parent 4f9a2d0b93
commit 2bb4aab6ac

@ -399,10 +399,12 @@ body .navbar .nodebb-inline-block {
.icon-circle { .icon-circle {
font-size: 12px; font-size: 12px;
color: green; color: green;
margin-right:3px;
} }
.icon-circle-blank { .icon-circle-blank {
font-size: 12px; font-size: 12px;
color: red; color: red;
margin-right:3px;
} }
} }

@ -86,9 +86,11 @@ var socket,
uid = el.parents('li').attr('data-uid'); uid = el.parents('li').attr('data-uid');
if (uid && jQuery.inArray(uid, users) !== -1) { if (uid && jQuery.inArray(uid, users) !== -1) {
el.prepend('<i class="icon-circle"></i>&nbsp;'); el.find('i').remove();
el.prepend('<i class="icon-circle"></i>');
} else { } else {
el.prepend('<i class="icon-circle-blank"></i>&nbsp;'); el.find('i').remove();
el.prepend('<i class="icon-circle-blank"></i>');
} }
el.processed = true; el.processed = true;
@ -208,9 +210,7 @@ var socket,
} }
}; };
app.process_page = function() { app.populate_online_users = function() {
function populate_online_users() {
var uids = []; var uids = [];
jQuery('.post-row').each(function() { jQuery('.post-row').each(function() {
@ -220,13 +220,15 @@ var socket,
socket.emit('api:user.get_online_users', uids); socket.emit('api:user.get_online_users', uids);
} }
app.process_page = function() {
// here is where all modules' onNavigate should be called, I think. // here is where all modules' onNavigate should be called, I think.
require(['mobileMenu'], function(mobileMenu) { require(['mobileMenu'], function(mobileMenu) {
mobileMenu.onNavigate(); mobileMenu.onNavigate();
}); });
populate_online_users(); app.populate_online_users();
setTimeout(function() { setTimeout(function() {
window.scrollTo(0, 1); // rehide address bar on mobile after page load completes. window.scrollTo(0, 1); // rehide address bar on mobile after page load completes.

@ -324,6 +324,8 @@
var activeEl = $('#thread_active_users'); var activeEl = $('#thread_active_users');
if(activeEl.length) if(activeEl.length)
activeEl.html(data); activeEl.html(data);
app.populate_online_users();
}); });
socket.on('event:rep_up', function(data) { socket.on('event:rep_up', function(data) {
@ -351,6 +353,7 @@
tempContainer.replaceWith(tempContainer.contents()); tempContainer.replaceWith(tempContainer.contents());
infiniteLoaderActive = false; infiniteLoaderActive = false;
app.populate_online_users();
addCommasToNumbers(); addCommasToNumbers();
}); });

@ -904,29 +904,6 @@ var utils = require('./../public/src/utils.js'),
} }
}; };
User.get_online_users = function(socket, uids) {
RDB.sismembers('users:online', uids, function(err, data) {
RDB.handle(err);
socket.emit('api:user.get_online_users', data);
});
};
User.go_online = function(uid) {
RDB.sadd('users:online', uid, function(err) {
if (err) {
RDB.handle(err);
}
});
};
User.go_offline = function(uid) {
RDB.srem('users:online', uid, function(err) {
if (err) {
RDB.handle(err);
}
});
};
User.active = { User.active = {
get_record : function(socket) { get_record : function(socket) {
RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(err, data) { RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(err, data) {

@ -58,43 +58,35 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
var hs = socket.handshake; var hs = socket.handshake;
var uid = users[hs.sessionID]; var uid = users[hs.sessionID];
// if (uid > 0) {
userSockets[uid] = userSockets[uid] || []; userSockets[uid] = userSockets[uid] || [];
userSockets[uid].push(socket); userSockets[uid].push(socket);
user.go_online(uid);
socket.join('uid_' + uid);
// }
/*process.on('uncaughtException', function(err) { socket.join('uid_' + uid);
// handle the error safely
console.log("error message "+err);
socket.emit('event:consolelog',{type:'uncaughtException', stack:err.stack, error:err.toString()});
});*/
socket.emit('event:connect', {status: 1}); socket.emit('event:connect', {status: 1});
socket.on('disconnect', function() { socket.on('disconnect', function() {
// if (uid > 0) {
user.go_offline(uid);
delete users[hs.sessionID];
var index = userSockets[uid].indexOf(socket); var index = userSockets[uid].indexOf(socket);
if(index !== -1) { if(index !== -1) {
userSockets[uid].splice(index, 1); userSockets[uid].splice(index, 1);
} }
if(userSockets[uid].length === 0)
delete users[hs.sessionID];
for(var roomName in rooms) { for(var roomName in rooms) {
socket.leave(roomName); socket.leave(roomName);
if(rooms[roomName][hs.sessionID]) { if(rooms[roomName][socket.id]) {
delete rooms[roomName][hs.sessionID]; delete rooms[roomName][socket.id];
} }
updateRoomBrowsingText(roomName); updateRoomBrowsingText(roomName);
} }
// }
}); });
socket.on('api:get_all_rooms', function(data) { socket.on('api:get_all_rooms', function(data) {
@ -105,9 +97,9 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
function getUidsInRoom(room) { function getUidsInRoom(room) {
var uids = []; var uids = [];
for(var sessionId in room) { for(var socketId in room) {
if(uids.indexOf(room[sessionId]) === -1) if(uids.indexOf(room[socketId]) === -1)
uids.push(room[sessionId]); uids.push(room[socketId]);
} }
return uids; return uids;
} }
@ -118,6 +110,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
for(var i=0; i<clients.length; ++i) { for(var i=0; i<clients.length; ++i) {
var hs = clients[i].handshake; var hs = clients[i].handshake;
if(hs && !users[hs.sessionID]) { if(hs && !users[hs.sessionID]) {
++anonCount; ++anonCount;
} }
@ -163,16 +156,16 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
rooms[data.enter] = rooms[data.enter] || {}; rooms[data.enter] = rooms[data.enter] || {};
if(data.leave) {
if (uid) { if (uid) {
rooms[data.enter][hs.sessionID] = uid; rooms[data.enter][socket.id] = uid;
if (data.leave && rooms[data.leave] && rooms[data.leave][hs.sessionID]) { if (data.leave && rooms[data.leave] && rooms[data.leave][socket.id]) {
delete rooms[data.leave][hs.sessionID]; delete rooms[data.leave][socket.id];
} }
} }
if(data.leave)
updateRoomBrowsingText(data.leave); updateRoomBrowsingText(data.leave);
}
updateRoomBrowsingText(data.enter); updateRoomBrowsingText(data.enter);
@ -185,7 +178,6 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
socket.on('api:updateHeader', function(data) { socket.on('api:updateHeader', function(data) {
if(uid) { if(uid) {
user.getUserFields(uid, data.fields, function(fields) { user.getUserFields(uid, data.fields, function(fields) {
fields.uid = uid; fields.uid = uid;
socket.emit('api:updateHeader', fields); socket.emit('api:updateHeader', fields);
@ -237,7 +229,16 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
}); });
socket.on('api:user.get_online_users', function(data) { socket.on('api:user.get_online_users', function(data) {
user.get_online_users(socket, data); var returnData = [];
for(var i=0; i<data.length; ++i) {
var uid = data[i];
if(userSockets[uid] && userSockets[uid].length > 0)
returnData.push(uid);
else
returnData.push(0);
}
socket.emit('api:user.get_online_users', returnData);
}); });
socket.on('api:user.changePassword', function(data) { socket.on('api:user.changePassword', function(data) {

Loading…
Cancel
Save