fix circular dependency -- involves indentations fix

v1.18.x
Aziz Khoury 9 years ago
parent 807e3a9d7e
commit c07e29bad6

@ -12,12 +12,11 @@ var logger = require('../logger');
var ratelimit = require('../middleware/ratelimit');
var cls = require('../middleware/cls');
var Sockets = {};
var Namespaces = {};
(function(Sockets) {
var Namespaces = {};
var io;
var io;
Sockets.init = function(server) {
Sockets.init = function (server) {
requireModules();
io = new SocketIO({
@ -37,46 +36,48 @@ Sockets.init = function(server) {
});
Sockets.server = io;
};
};
function onConnection(socket) {
function onConnection(socket) {
socket.ip = socket.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
logger.io_one(socket, socket.uid);
cls.socket(socket, null, 'connection', function() {
cls.socket(socket, null, 'connection', function () {
onConnect(socket);
});
socket.on('*', function(payload) {
cls.socket(socket, payload, null, function() {
socket.on('*', function (payload) {
cls.socket(socket, payload, null, function () {
onMessage(socket, payload);
});
});
}
}
function onConnect(socket) {
function onConnect(socket) {
if (socket.uid) {
socket.join('uid_' + socket.uid);
socket.join('online_users');
} else {
socket.join('online_guests');
}
}
}
function onDisconnect(socket) {
cls.socket(socket, null, 'disconnect', function() {});
}
function onDisconnect(socket) {
cls.socket(socket, null, 'disconnect', function () {
});
}
function onMessage(socket, payload) {
function onMessage(socket, payload) {
if (!payload.data.length) {
return winston.warn('[socket.io] Empty payload');
}
var eventName = payload.data[0];
var params = payload.data[1];
var callback = typeof payload.data[payload.data.length - 1] === 'function' ? payload.data[payload.data.length - 1] : function() {};
var callback = typeof payload.data[payload.data.length - 1] === 'function' ? payload.data[payload.data.length - 1] : function () {
};
if (!eventName) {
return winston.warn('[socket.io] Empty method name');
@ -84,7 +85,7 @@ function onMessage(socket, payload) {
var parts = eventName.toString().split('.');
var namespace = parts[0];
var methodToCall = parts.reduce(function(prev, cur) {
var methodToCall = parts.reduce(function (prev, cur) {
if (prev !== null && prev[cur]) {
return prev[cur];
} else {
@ -92,7 +93,7 @@ function onMessage(socket, payload) {
}
}, Namespaces);
if(!methodToCall) {
if (!methodToCall) {
if (process.env.NODE_ENV === 'development') {
winston.warn('[socket.io] Unrecognized message: ' + eventName);
}
@ -124,36 +125,36 @@ function onMessage(socket, payload) {
function (next) {
methodToCall(socket, params, next);
}
], function(err, result) {
], function (err, result) {
callback(err ? {message: err.message} : null, result);
});
}
}
function requireModules() {
function requireModules() {
var modules = ['admin', 'categories', 'groups', 'meta', 'modules',
'notifications', 'plugins', 'posts', 'topics', 'user', 'blacklist'
];
modules.forEach(function(module) {
modules.forEach(function (module) {
Namespaces[module] = require('./' + module);
});
}
}
function validateSession(socket, callback) {
function validateSession(socket, callback) {
var req = socket.request;
if (!req.signedCookies || !req.signedCookies['express.sid']) {
return callback(new Error('[[error:invalid-session]]'));
}
db.sessionStore.get(req.signedCookies['express.sid'], function(err, sessionData) {
db.sessionStore.get(req.signedCookies['express.sid'], function (err, sessionData) {
if (err || !sessionData) {
return callback(err || new Error('[[error:invalid-session]]'));
}
callback();
});
}
}
function authorize(socket, callback) {
function authorize(socket, callback) {
var request = socket.request;
if (!request) {
@ -161,11 +162,11 @@ function authorize(socket, callback) {
}
async.waterfall([
function(next) {
function (next) {
cookieParser(request, {}, next);
},
function(next) {
db.sessionStore.get(request.signedCookies['express.sid'], function(err, sessionData) {
function (next) {
db.sessionStore.get(request.signedCookies['express.sid'], function (err, sessionData) {
if (err) {
return next(err);
}
@ -179,9 +180,9 @@ function authorize(socket, callback) {
});
}
], callback);
}
}
function addRedisAdapter(io) {
function addRedisAdapter(io) {
if (nconf.get('redis')) {
var redisAdapter = require('socket.io-redis');
var redis = require('../database/redis');
@ -192,23 +193,23 @@ function addRedisAdapter(io) {
} else if (nconf.get('isCluster') === 'true') {
winston.warn('[socket.io] Clustering detected, you are advised to configure Redis as a websocket store.');
}
}
}
Sockets.in = function(room) {
Sockets.in = function (room) {
return io.in(room);
};
};
Sockets.getUserSocketCount = function(uid) {
Sockets.getUserSocketCount = function (uid) {
if (!io) {
return 0;
}
var room = io.sockets.adapter.rooms['uid_' + uid];
return room ? room.length : 0;
};
};
Sockets.reqFromSocket = function(socket) {
Sockets.reqFromSocket = function (socket) {
var headers = socket.request.headers;
var host = headers.host;
var referer = headers.referer || '';
@ -222,7 +223,6 @@ Sockets.reqFromSocket = function(socket) {
path: referer.substr(referer.indexOf(host) + host.length),
headers: headers
};
};
};
module.exports = Sockets;
})(exports);
Loading…
Cancel
Save