unify request as a store key for both http and websockets calls

v1.18.x
Aziz Khoury 9 years ago
parent 509676fdf4
commit 53e9627001

@ -1,4 +1,6 @@
var path = require('path');
var sockets = require('path');
var websockets = require('../socket.io');
var continuationLocalStorage = require('continuation-local-storage');
var APP_NAMESPACE = require(path.join(__dirname, '../../package.json')).name;
var namespace = continuationLocalStorage.createNamespace(APP_NAMESPACE);
@ -7,18 +9,14 @@ var cls = {};
cls.http = function (req, res, next) {
namespace.run(function() {
namespace.set('http', {req: req, res: res});
namespace.set('request', req);
next();
});
};
cls.socket = function (socket, payload, event, next) {
namespace.run(function() {
namespace.set('ws', {
socket: socket,
payload: payload,
// if it's a null event, then we grab it from the payload
event: event || ((payload || {}).data || [])[0]});
namespace.set('request', websockets.reqFromSocket(socket, payload, event));
next();
});
};
@ -33,7 +31,6 @@ cls.set = function (key, value) {
cls.setItem = cls.set;
cls.getItem = cls.set;
cls.getNamespace = cls.storage;
cls.namespace = namespace;
cls.continuationLocalStorage = continuationLocalStorage;

@ -248,7 +248,7 @@ Sockets.getOnlineAnonCount = function () {
return room ? room.length : 0;
};
Sockets.reqFromSocket = function(socket) {
Sockets.reqFromSocket = function(socket, payload, event) {
var headers = socket.request.headers;
var host = headers.host;
var referer = headers.referer || '';
@ -256,11 +256,14 @@ Sockets.reqFromSocket = function(socket) {
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
headers: headers,
_socket: socket
};
};

Loading…
Cancel
Save