fix #28 enable to access http headers

This commit is contained in:
Naoyuki Kanezawa
2014-08-17 06:03:58 +09:00
parent 65813b5859
commit 026f363707
3 changed files with 111 additions and 0 deletions

View File

@@ -64,6 +64,32 @@ io.of(nsp).on('connection', function(socket) {
});
});
function before(context, name, fn) {
var method = context[name];
context[name] = function() {
fn.apply(this, arguments);
return method.apply(this, arguments);
};
}
before(io.eio, 'handleRequest', function(req, res) {
// echo a header value
var value = req.headers['x-socketio'];
if (!value) return;
res.setHeader('X-SocketIO', value);
});
before(io.eio, 'handleUpgrade', function(req, socket, head) {
// echo a header value for websocket handshake
var value = req.headers['x-socketio'];
if (!value) return;
this.ws.once('headers', function(headers) {
headers.push('X-SocketIO: ' + value);
});
});
server.listen(port, function() {
console.log('Socket.IO server listening on port', port);
});