3

In the ws Node.js WebSocket library, regarding the event socket.on('close', ...), is there any direct way (i. e. by inspecting the event) to determine if the client has closed the socket OR the server closed it for this client?

Even when the server explicitly sets a specific close code (4001), the handler on the server side for the close event gets a generic close code (1005).

So there is no way around but to remember it somewhere if the server closed the socket?

Amaterasu
  • 153
  • 10

1 Answers1

2

If the client closed it, it knows it closed it. But if it doesn't then it must be server. The same thing can happen server-side

Sive
  • 79
  • 11
  • But how does the handler for the close event know it? The handler gets called regardless. So it seems it must be remembered somewhere else. – Amaterasu Jan 22 '22 at 10:39
  • from everywhere in the client code, wherever the connection can/will be closed, just put this line of code somewhere below it: localstorage["closedBY"] = "client" (may defer language to language) – Sive Jan 23 '22 at 17:10