From 49362abcd0c176ed0f8621bc70820a8adf9859b2 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 11 Oct 2017 07:29:52 +0200 Subject: [PATCH] [fix] Fix undefined remoteAddress when using uws (#533) Fixes https://github.com/socketio/socket.io/issues/2982 --- lib/socket.js | 6 +++++- test/server.js | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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(); + }); + }); + }); + }); });