websockets.js to follow same pattern to avoid circular dependencies

v1.18.x
akhoury 11 years ago
parent 1aa1ddb4ec
commit 0f53749e70

@ -5,6 +5,7 @@ var cookie = require('cookie'),
async = require('async'), async = require('async'),
fs = require('fs'), fs = require('fs'),
nconf = require('nconf'), nconf = require('nconf'),
gravatar = require('gravatar'),
winston = require('winston'), winston = require('winston'),
RedisStoreLib = require('connect-redis')(express), RedisStoreLib = require('connect-redis')(express),
@ -34,12 +35,13 @@ var cookie = require('cookie'),
}, },
plugins = require('./plugins'); plugins = require('./plugins');
(function(websockets) {
var users = {}, var users = {},
userSockets = {}, userSockets = {},
rooms = {}; rooms = {};
module.exports.logoutUser = function(uid) { websockets.logoutUser = function(uid) {
if(userSockets[uid] && userSockets[uid].length) { if(userSockets[uid] && userSockets[uid].length) {
for(var i=0; i< userSockets[uid].length; ++i) { for(var i=0; i< userSockets[uid].length; ++i) {
userSockets[uid][i].emit('event:disconnect'); userSockets[uid][i].emit('event:disconnect');
@ -55,9 +57,9 @@ module.exports.logoutUser = function(uid) {
function isUserOnline(uid) { function isUserOnline(uid) {
return !!userSockets[uid] && userSockets[uid].length > 0; return !!userSockets[uid] && userSockets[uid].length > 0;
} }
module.exports.isUserOnline = isUserOnline; websockets.isUserOnline = isUserOnline;
module.exports.init = function(io) { websockets.exports.init = function(io) {
io.sockets.on('connection', function(socket) { io.sockets.on('connection', function(socket) {
var hs = socket.handshake, var hs = socket.handshake,
@ -222,9 +224,9 @@ module.exports.init = function(io) {
uid: 0, uid: 0,
username: "Anonymous User", username: "Anonymous User",
email: '', email: '',
picture: require('gravatar').url('', { picture: gravatar.url('', {
s: '24' s: '24'
}, https = nconf.get('https')) }, nconf.get('https'))
}); });
} }
@ -475,12 +477,12 @@ module.exports.init = function(io) {
}); });
socket.on('api:user.getOnlineAnonCount', function(data, callback) { socket.on('api:user.getOnlineAnonCount', function(data, callback) {
callback(module.exports.getOnlineAnonCount()); callback(websockets.getOnlineAnonCount());
}); });
module.exports.getOnlineAnonCount = function () { websockets.getOnlineAnonCount = function () {
return userSockets[0] ? userSockets[0].length : 0; return userSockets[0] ? userSockets[0].length : 0;
} };
function emitOnlineUserCount() { function emitOnlineUserCount() {
var anon = userSockets[0] ? userSockets[0].length : 0; var anon = userSockets[0] ? userSockets[0].length : 0;
@ -947,7 +949,7 @@ module.exports.init = function(io) {
userSockets[theirid][i].emit('event:banned'); userSockets[theirid][i].emit('event:banned');
} }
} }
module.exports.logoutUser(theirid); websockets.logoutUser(theirid);
} }
}); });
} }
@ -1114,7 +1116,7 @@ module.exports.init = function(io) {
}); });
} }
module.exports.emitUserCount = function() { websockets.emitUserCount = function() {
RDB.get('usercount', function(err, count) { RDB.get('usercount', function(err, count) {
io.sockets.emit('user.count', { io.sockets.emit('user.count', {
count: count count: count
@ -1122,9 +1124,10 @@ module.exports.init = function(io) {
}); });
}; };
module.exports.in = function(room) { websockets.in = function(room) {
return io.sockets.in(room); return io.sockets.in(room);
}; };
} }
})(module.exports);

Loading…
Cancel
Save