diff --git a/lib/socket.js b/lib/socket.js index 526707c26..177b25c69 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -31,7 +31,11 @@ function Socket (id, server, transport, req) { this.request = req; // Cache IP since it might not be in the req later - this.remoteAddress = req.connection.remoteAddress; + if (req.websocket && req.websocket._socket) { + this.remoteAddress = req.websocket._socket.remoteAddress; + } else { + this.remoteAddress = req.connection.remoteAddress; + } this.checkIntervalTimer = null; this.upgradeTimeoutTimer = null; diff --git a/test/server.js b/test/server.js index 50164b81d..59bb41d2f 100644 --- a/test/server.js +++ b/test/server.js @@ -2651,4 +2651,26 @@ describe('server', function () { }); }); }); + + describe('remoteAddress', function () { + it('should be defined (polling)', function (done) { + var engine = listen({ transports: ['polling'] }, port => { + eioc('ws://localhost:%d'.s(port), { transports: ['polling'] }); + engine.on('connection', socket => { + expect(socket.remoteAddress).to.be('::ffff:127.0.0.1'); + done(); + }); + }); + }); + + it('should be defined (ws)', function (done) { + var engine = listen({ transports: ['websocket'] }, port => { + eioc('ws://localhost:%d'.s(port), { transports: ['websocket'] }); + engine.on('connection', socket => { + expect(socket.remoteAddress).to.be('::ffff:127.0.0.1'); + done(); + }); + }); + }); + }); });