Ws: How get IP address?

Created on 11 Oct 2016  路  11Comments  路  Source: websockets/ws

How get IP address before connecting?

var webSocketServer = new WebSocketServer.Server({port: 8081, verifyClient: function(info, callback) { });

Most helpful comment

It should be info.req.connection.remoteAddress but if you are using a reverse proxy that is the proxy address.

All 11 comments

The info object should contain a reference to the request object.

info.request ?

info.req where req is the full request object.

I don't find IP address client on server -

console.log(info.req);
 console.log(util.inspect(info, false, null));

Been at this _exact_ spot for 2 hours now! Google, SO, WS docs, everything. :weary:

  • info.req.remoteAddr is undefined
  • info.req.connection.remoteAddr is undefined
  • info.req.socket.remoteAddr is undefined
  • info.req.connection.socket is undefined
  • info.req.socket._socket is undefined
  • info.req.socket.upgradeReq is undefined

EDIT: My bad... Should be .remoteAddress not .remoteAddr

It should be info.req.connection.remoteAddress but if you are using a reverse proxy that is the proxy address.

IKR? If req is a copy of the Node.js http.ClientRequest, remoteAddr should be at req.connection.remoteAddr, except it's not...

new ws.Server({
    server,          // (server instanceof http.Server) === true
    verifyClient(info) {
        console.log(info.req.connection.remoteAddr);
        return true;
    },
});

Yields


on connection.

Can someone reproduce?

EDIT: Should be remoteAddress not remoteAddr...

Since when was it connection.remoteAddr? The Node.js docs clearly state connection.remoteAddress

Oh damn... I'm embarrassed now! Sorry. My bad. I have been reading that as remoteAddr, even when @Ipinca commented above. So sorry...

lpinca - Very Thanks You!!!

np.

Was this page helpful?
0 / 5 - 0 ratings