what did i do?

v1.18.x
Aziz Khoury 9 years ago
parent 01ee020750
commit 4f3a962f7f

@ -8,17 +8,15 @@ var cookieParser = require('cookie-parser')(nconf.get('secret'));
var winston = require('winston');
var db = require('../database');
var user = require('../user');
var logger = require('../logger');
var ratelimit = require('../middleware/ratelimit');
var cls = require('../middleware/cls');
var io;
var Sockets = {};
var Namespaces = {};
(function(Sockets) {
var Namespaces = {};
var io;
Sockets.init = function(server) {
Sockets.init = function(server) {
requireModules();
io = new SocketIO({
@ -32,48 +30,36 @@ var io;
io.on('connection', onConnection);
io.on('disconnect', function(data) {
onDisconnect(io, data);
});
io.listen(server, {
transports: nconf.get('socket.io:transports')
});
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 () {
onConnect(socket);
});
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 onMessage(socket, payload) {
function onMessage(socket, payload) {
if (!payload.data.length) {
return winston.warn('[socket.io] Empty payload');
}
@ -131,9 +117,9 @@ var io;
], 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'
];
@ -141,9 +127,9 @@ var io;
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]]'));
@ -155,9 +141,9 @@ var io;
callback();
});
}
}
function authorize(socket, callback) {
function authorize(socket, callback) {
var request = socket.request;
if (!request) {
@ -183,9 +169,9 @@ var io;
});
}
], callback);
}
}
function addRedisAdapter(io) {
function addRedisAdapter(io) {
if (nconf.get('redis')) {
var redisAdapter = require('socket.io-redis');
var redis = require('../database/redis');
@ -196,23 +182,23 @@ var 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, payload, event) {
Sockets.reqFromSocket = function(socket) {
var headers = socket.request.headers;
var host = headers.host;
var referer = headers.referer || '';
@ -220,15 +206,13 @@ var io;
return {
ip: headers['x-forwarded-for'] || socket.ip,
host: host,
uid: socket.uid,
protocol: socket.request.connection.encrypted ? 'https' : 'http',
secure: !!socket.request.connection.encrypted,
url: referer,
body: {event: event || ((payload || {}).data || [])[0], payload: payload},
path: referer.substr(referer.indexOf(host) + host.length),
headers: headers,
_socket: socket
};
headers: headers
};
};
})(exports);
module.exports = Sockets;
Loading…
Cancel
Save