ws icon indicating copy to clipboard operation
ws copied to clipboard

on('close') sets underlying socket to null?

Open christofferTP opened this issue 9 years ago • 6 comments

If i have a wss connection and it terminates, for instance in the below code:

connection.on('close', function(reasonCode, description) {
      console.log(reasonCode, description);
      const address = connection._socket.remoteAddress; // Doesnt work - socket is null
      console.log('[' +new Date() + ']'
        +" EXTERNAL: Disconnected: "
        + address );
      registry.remove_device(address);

I get the following on close:

/home/chritof/Documents/vbox/wsh.js:232
      const address = connection._socket.remoteAddress;
                                        ^

TypeError: Cannot read property 'remoteAddress' of null

This seems like a bad design decision, why cant I access the socket and its data ? I need to remove this information from my map..

Regards

christofferTP avatar Feb 17 '17 11:02 christofferTP

Yes, a cleanup process is run before emitting the close event and it sets the underlying socket to null. You shouldn't rely on _socket as it is a private property.

lpinca avatar Feb 17 '17 11:02 lpinca

How do you suggest I get a hold of the connection information in the on close method?

christofferTP avatar Feb 17 '17 11:02 christofferTP

This is on the server right? If so use the public upgradeReq property.

lpinca avatar Feb 17 '17 11:02 lpinca

Yes, on the server. Will look at retrieving IP from the upgradeReq

christofferTP avatar Feb 17 '17 11:02 christofferTP

ws.upgradeReq.connection.remoteAddress

should work. If your server is behind a proxy use the X-Forwarded-For header or whatever you use.

lpinca avatar Feb 17 '17 11:02 lpinca

Nice, that worked. Thanks.

christofferTP avatar Feb 17 '17 11:02 christofferTP