From 0f53749e70f69801f4e9bb7f894c8b0572024237 Mon Sep 17 00:00:00 2001 From: akhoury Date: Sun, 1 Dec 2013 10:53:59 -0500 Subject: [PATCH] websockets.js to follow same pattern to avoid circular dependencies --- src/websockets.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/websockets.js b/src/websockets.js index 3f2fb5e4a3..87af2b8029 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -5,6 +5,7 @@ var cookie = require('cookie'), async = require('async'), fs = require('fs'), nconf = require('nconf'), + gravatar = require('gravatar'), winston = require('winston'), RedisStoreLib = require('connect-redis')(express), @@ -34,12 +35,13 @@ var cookie = require('cookie'), }, plugins = require('./plugins'); +(function(websockets) { var users = {}, userSockets = {}, rooms = {}; -module.exports.logoutUser = function(uid) { +websockets.logoutUser = function(uid) { if(userSockets[uid] && userSockets[uid].length) { for(var i=0; i< userSockets[uid].length; ++i) { userSockets[uid][i].emit('event:disconnect'); @@ -55,9 +57,9 @@ module.exports.logoutUser = function(uid) { function isUserOnline(uid) { 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) { var hs = socket.handshake, @@ -222,9 +224,9 @@ module.exports.init = function(io) { uid: 0, username: "Anonymous User", email: '', - picture: require('gravatar').url('', { + picture: gravatar.url('', { 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) { - callback(module.exports.getOnlineAnonCount()); + callback(websockets.getOnlineAnonCount()); }); - module.exports.getOnlineAnonCount = function () { + websockets.getOnlineAnonCount = function () { return userSockets[0] ? userSockets[0].length : 0; - } + }; function emitOnlineUserCount() { var anon = userSockets[0] ? userSockets[0].length : 0; @@ -947,7 +949,7 @@ module.exports.init = function(io) { 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) { io.sockets.emit('user.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); }; } +})(module.exports);