diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 664f11dd5b..73eb9ca8eb 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -23,10 +23,13 @@ var SocketIO = require('socket.io'), /* === */ var users = {}, - userSockets = {}, - rooms = {}, io; + +Sockets.userSockets = {}; +Sockets.rooms = {}; + + Sockets.init = function() { io = socketioWildcard(SocketIO).listen(global.server, { @@ -35,6 +38,8 @@ Sockets.init = function() { 'browser client minification': true }); + Sockets.server = io; + fs.readdir(__dirname, function(err, files) { files.splice(files.indexOf('index.js'), 1); @@ -59,8 +64,10 @@ Sockets.init = function() { uid = users[sessionID] = 0; } - userSockets[uid] = userSockets[uid] || []; - userSockets[uid].push(socket); + socket.uid = uid; + + 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 = { @@ -93,14 +100,14 @@ Sockets.init = function() { socket.on('disconnect', function() { - var index = (userSockets[uid] || []).indexOf(socket); + var index = (Sockets.userSockets[uid] || []).indexOf(socket); if (index !== -1) { - userSockets[uid].splice(index, 1); + Sockets.userSockets[uid].splice(index, 1); } - if (userSockets[uid] && userSockets[uid].length === 0) { + if (Sockets.userSockets[uid] && Sockets.userSockets[uid].length === 0) { delete users[sessionID]; - delete userSockets[uid]; + delete Sockets.userSockets[uid]; if (uid) { db.sortedSetRemove('users:online', uid, function(err, data) { }); @@ -111,12 +118,12 @@ Sockets.init = function() { emitOnlineUserCount(); - for (var roomName in rooms) { - if (rooms.hasOwnProperty(roomName)) { + for (var roomName in Sockets.rooms) { + if (Sockets.rooms.hasOwnProperty(roomName)) { socket.leave(roomName); - if (rooms[roomName][socket.id]) { - delete rooms[roomName][socket.id]; + if (Sockets.rooms[roomName][socket.id]) { + delete Sockets.rooms[roomName][socket.id]; } updateRoomBrowsingText(roomName); @@ -125,72 +132,48 @@ Sockets.init = function() { }); socket.on('*', function(payload, callback) { - // Ignore all non-api messages - if (payload.name.substr(0, 4) !== 'api:') { - return; - } else { - // Deconstruct the message - var parts = payload.name.slice(4).split('.'), - namespace = parts.slice(0, 1), - methodToCall = parts.reduce(function(prev, cur) { - if (prev !== null && prev[cur]) { - return prev[cur]; - } else { - return null; - } - }, Namespaces); - - if (methodToCall !== null) { - var sessionData = { - uid: uid, - socket: socket, - rooms: rooms, - server: io, - userSockets: userSockets - }, - socketArgs = []; - - // Construct the arguments that'll get passed into each socket method - if (payload.args.length) { - socketArgs = socketArgs.concat(payload.args); - } - if (callback !== undefined) { - socketArgs.push(callback); + function callMethod(method) { + method.call(socket, args[0]?args[0]:null, function(err, result) { + if(callback) { + callback(err?{message:err.message}:null, result); } - socketArgs.push(sessionData); - - // Call the requested method - if (Namespaces[namespace].before) { - Namespaces[namespace].before(sessionData, function() { - try { - methodToCall.apply(Namespaces, socketArgs); - } catch (e) { - winston.error(e.message); - } - }); + }); + } + + + var parts = payload.name.split('.'), + namespace = parts.slice(0, 1), + methodToCall = parts.reduce(function(prev, cur) { + if (prev !== null && prev[cur]) { + return prev[cur]; } else { - try { - methodToCall.apply(Namespaces, socketArgs); - } catch (e) { - winston.error(e.message); - } + return null; } - // winston.info('[socket.io] Executing: ' + payload.name); + }, Namespaces); + + if (methodToCall !== null) { + + if (Namespaces[namespace].before) { + Namespaces[namespace].before(socket, function() { + callMethod(methodToCall); + }); } else { - winston.warn('[socket.io] Unrecognized message: ' + payload.name); + callMethod(methodToCall); } + } else { + winston.warn('[socket.io] Unrecognized message: ' + payload.name); } }); }); }; Sockets.logoutUser = function(uid) { - if(userSockets[uid] && userSockets[uid].length) { - for(var i=0; i< userSockets[uid].length; ++i) { - userSockets[uid][i].emit('event:disconnect'); - userSockets[uid][i].disconnect(); + 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(!userSockets[uid]) { + if(!Sockets.userSockets[uid]) { return; } } @@ -211,18 +194,18 @@ Sockets.in = function(room) { }; Sockets.getConnectedClients = function() { - return userSockets; + return Sockets.userSockets; }; Sockets.getOnlineAnonCount = function () { - return userSockets[0] ? userSockets[0].length : 0; + return Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0; }; /* Helpers */ Sockets.isUserOnline = isUserOnline; function isUserOnline(uid) { - return !!userSockets[uid] && userSockets[uid].length > 0; + return !!Sockets.userSockets[uid] && Sockets.userSockets[uid].length > 0; } Sockets.updateRoomBrowsingText = updateRoomBrowsingText; @@ -251,7 +234,7 @@ function updateRoomBrowsingText(roomName) { return anonCount; } - var uids = getUidsInRoom(rooms[roomName]), + var uids = getUidsInRoom(Sockets.rooms[roomName]), anonymousCount = getAnonymousCount(roomName); if (uids.length === 0) { @@ -287,8 +270,8 @@ function emitTopicPostStats(callback) { Sockets.emitOnlineUserCount = emitOnlineUserCount; function emitOnlineUserCount(callback) { - var anon = userSockets[0] ? userSockets[0].length : 0; - var registered = Object.keys(userSockets).length; + var anon = Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0; + var registered = Object.keys(Sockets.userSockets).length; if (anon) { registered = registered - 1; }