on('close') sets underlying socket to null?
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
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.
How do you suggest I get a hold of the connection information in the on close method?
This is on the server right? If so use the public upgradeReq property.
Yes, on the server. Will look at retrieving IP from the upgradeReq
ws.upgradeReq.connection.remoteAddress
should work. If your server is behind a proxy use the X-Forwarded-For header or whatever you use.
Nice, that worked. Thanks.