This commit is contained in:
barisusakli
2014-11-28 14:35:21 -05:00
7 changed files with 106 additions and 137 deletions

View File

@@ -24,47 +24,6 @@ var SocketIO = require('socket.io'),
var io;
var onlineUsers = [];
process.on('message', onMessage);
function onMessage(msg) {
if (typeof msg !== 'object') {
return;
}
if (msg.action === 'user:connect') {
if (msg.uid && onlineUsers.indexOf(msg.uid) === -1) {
onlineUsers.push(msg.uid);
}
} else if(msg.action === 'user:disconnect') {
if (msg.uid && msg.socketCount <= 1) {
var index = onlineUsers.indexOf(msg.uid);
if (index !== -1) {
onlineUsers.splice(index, 1);
}
}
}
}
function onUserConnect(uid, socketid) {
var msg = {action: 'user:connect', uid: uid, socketid: socketid};
if (process.send) {
process.send(msg);
} else {
onMessage(msg);
}
}
function onUserDisconnect(uid, socketid, socketCount) {
var msg = {action: 'user:disconnect', uid: uid, socketid: socketid, socketCount: socketCount};
if (process.send) {
process.send(msg);
} else {
onMessage(msg);
}
}
Sockets.init = function(server) {
var config = {
transports: ['polling', 'websocket'],
@@ -131,7 +90,6 @@ Sockets.init = function(server) {
logger.io_one(socket, socket.uid);
if (socket.uid) {
onUserConnect(socket.uid, socket.id);
socket.join('uid_' + socket.uid);
socket.join('online_users');
@@ -174,8 +132,6 @@ Sockets.init = function(server) {
socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'});
}
onUserDisconnect(socket.uid, socket.id, socketCount);
// for(var roomName in io.sockets.manager.roomClients[socket.id]) {
// if (roomName.indexOf('topic') !== -1) {
// io.sockets.in(roomName.slice(1)).emit('event:user_leave', socket.uid);
@@ -291,10 +247,6 @@ Sockets.getSocketCount = function() {
return Array.isArray(clients) ? clients.length : 0;
};
Sockets.getConnectedClients = function() {
return onlineUsers;
};
Sockets.getUserSocketCount = function(uid) {
return 0;
@@ -305,10 +257,6 @@ Sockets.getUserSocketCount = function(uid) {
return roomClients.length;
};
Sockets.getOnlineUserCount = function () {
return onlineUsers.length;
};
Sockets.getOnlineAnonCount = function () {
return 0;

View File

@@ -86,62 +86,67 @@ SocketMeta.rooms.enter = function(socket, data, callback) {
};
SocketMeta.rooms.getAll = function(socket, data, callback) {
var rooms = {}; //websockets.server.sockets.manager.rooms; doesnt work in socket.io 1.x
var socketData = {
onlineGuestCount: websockets.getOnlineAnonCount(),
onlineRegisteredCount: websockets.getOnlineUserCount(),
socketCount: websockets.getSocketCount(),
users: {
home: rooms['/home'] ? rooms['/home'].length : 0,
topics: 0,
category: 0
},
topics: {}
};
var scores = {},
topTenTopics = [],
tid;
for (var room in rooms) {
if (rooms.hasOwnProperty(room)) {
if (tid = room.match(/^\/topic_(\d+)/)) {
var length = rooms[room].length;
socketData.users.topics += length;
if (scores[length]) {
scores[length].push(tid[1]);
} else {
scores[length] = [tid[1]];
}
} else if (room.match(/^\/category/)) {
socketData.users.category += rooms[room].length;
}
}
}
var scoreKeys = Object.keys(scores),
mostActive = scoreKeys.sort();
while(topTenTopics.length < 10 && mostActive.length > 0) {
topTenTopics = topTenTopics.concat(scores[mostActive.pop()]);
}
topTenTopics = topTenTopics.slice(0, 10);
topics.getTopicsFields(topTenTopics, ['title'], function(err, titles) {
db.sortedSetCount('users:online', now - 300000, now, function(err, onlineRegisteredCount) {
if (err) {
return callback(err);
}
topTenTopics.forEach(function(tid, id) {
socketData.topics[tid] = {
value: rooms['/topic_' + tid].length,
title: validator.escape(titles[id].title)
};
});
callback(null, socketData);
var rooms = {}; //websockets.server.sockets.manager.rooms; doesnt work in socket.io 1.x
var socketData = {
onlineGuestCount: websockets.getOnlineAnonCount(),
onlineRegisteredCount: onlineRegisteredCount,
socketCount: websockets.getSocketCount(),
users: {
home: rooms['/home'] ? rooms['/home'].length : 0,
topics: 0,
category: 0
},
topics: {}
};
var scores = {},
topTenTopics = [],
tid;
for (var room in rooms) {
if (rooms.hasOwnProperty(room)) {
if (tid = room.match(/^\/topic_(\d+)/)) {
var length = rooms[room].length;
socketData.users.topics += length;
if (scores[length]) {
scores[length].push(tid[1]);
} else {
scores[length] = [tid[1]];
}
} else if (room.match(/^\/category/)) {
socketData.users.category += rooms[room].length;
}
}
}
var scoreKeys = Object.keys(scores),
mostActive = scoreKeys.sort();
while(topTenTopics.length < 10 && mostActive.length > 0) {
topTenTopics = topTenTopics.concat(scores[mostActive.pop()]);
}
topTenTopics = topTenTopics.slice(0, 10);
topics.getTopicsFields(topTenTopics, ['title'], function(err, titles) {
if (err) {
return callback(err);
}
topTenTopics.forEach(function(tid, id) {
socketData.topics[tid] = {
value: rooms['/topic_' + tid].length,
title: validator.escape(titles[id].title)
};
});
callback(null, socketData);
});
});
};

View File

@@ -46,21 +46,24 @@ SocketPosts.reply = function(socket, data, callback) {
socket.emit('event:new_post', result);
var uids = websockets.getConnectedClients();
privileges.categories.filterUids('read', postData.topic.cid, uids, function(err, uids) {
user.getUidsFromSet('users:online', 0, -1, function(err, uids) {
if (err) {
return;
}
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: "newPost"}, function(err, data) {
uids = data.uidsTo;
for(var i=0; i<uids.length; ++i) {
if (parseInt(uids[i], 10) !== socket.uid) {
websockets.in('uid_' + uids[i]).emit('event:new_post', result);
}
privileges.categories.filterUids('read', postData.topic.cid, uids, function(err, uids) {
if (err) {
return;
}
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: "newPost"}, function(err, data) {
uids = data.uidsTo;
for(var i=0; i<uids.length; ++i) {
if (parseInt(uids[i], 10) !== socket.uid) {
websockets.in('uid_' + uids[i]).emit('event:new_post', result);
}
}
});
});
});
});

View File

@@ -41,22 +41,26 @@ SocketTopics.post = function(socket, data, callback) {
socket.emit('event:new_post', {posts: [result.postData]});
socket.emit('event:new_topic', result.topicData);
var uids = websockets.getConnectedClients();
privileges.categories.filterUids('read', result.topicData.cid, uids, function(err, uids) {
user.getUidsFromSet('users:online', 0, -1, function(err, uids) {
if (err) {
return;
}
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: "newTopic"}, function(err, data) {
uids = data.uidsTo;
for(var i=0; i<uids.length; ++i) {
if (parseInt(uids[i], 10) !== socket.uid) {
websockets.in('uid_' + uids[i]).emit('event:new_post', {posts: [result.postData]});
websockets.in('uid_' + uids[i]).emit('event:new_topic', result.topicData);
}
privileges.categories.filterUids('read', result.topicData.cid, uids, function(err, uids) {
if (err) {
return;
}
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: data.uid, type: "newTopic"}, function(err, data) {
uids = data.uidsTo;
for(var i=0; i<uids.length; ++i) {
if (parseInt(uids[i], 10) !== socket.uid) {
websockets.in('uid_' + uids[i]).emit('event:new_post', {posts: [result.postData]});
websockets.in('uid_' + uids[i]).emit('event:new_topic', result.topicData);
}
}
});
});
});
});