Skip to content

Commit 49362ab

Browse files
[fix] Fix undefined remoteAddress when using uws (#533)
Fixes socketio/socket.io#2982
1 parent 3c77780 commit 49362ab

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/socket.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ function Socket (id, server, transport, req) {
3131
this.request = req;
3232

3333
// Cache IP since it might not be in the req later
34-
this.remoteAddress = req.connection.remoteAddress;
34+
if (req.websocket && req.websocket._socket) {
35+
this.remoteAddress = req.websocket._socket.remoteAddress;
36+
} else {
37+
this.remoteAddress = req.connection.remoteAddress;
38+
}
3539

3640
this.checkIntervalTimer = null;
3741
this.upgradeTimeoutTimer = null;

test/server.js

+22
Original file line numberDiff line numberDiff line change
@@ -2651,4 +2651,26 @@ describe('server', function () {
26512651
});
26522652
});
26532653
});
2654+
2655+
describe('remoteAddress', function () {
2656+
it('should be defined (polling)', function (done) {
2657+
var engine = listen({ transports: ['polling'] }, port => {
2658+
eioc('ws://localhost:%d'.s(port), { transports: ['polling'] });
2659+
engine.on('connection', socket => {
2660+
expect(socket.remoteAddress).to.be('::ffff:127.0.0.1');
2661+
done();
2662+
});
2663+
});
2664+
});
2665+
2666+
it('should be defined (ws)', function (done) {
2667+
var engine = listen({ transports: ['websocket'] }, port => {
2668+
eioc('ws://localhost:%d'.s(port), { transports: ['websocket'] });
2669+
engine.on('connection', socket => {
2670+
expect(socket.remoteAddress).to.be('::ffff:127.0.0.1');
2671+
done();
2672+
});
2673+
});
2674+
});
2675+
});
26542676
});

0 commit comments

Comments
 (0)