I
I
iNeedHelp2016-07-23 23:06:49
Node.js
iNeedHelp, 2016-07-23 23:06:49

How to properly close a websocket?

Hello. For sockets I use the ws module. As soon as the user enters the site, I execute const ws = new WebSocket('ws://localhost:9000');. This ws variable is then sent to the server and added to the array where all clients are stored. Accordingly, when the user left the site, I need to somehow remove one element from this array. How to implement it?
I tried to hang a handler on the client ws.onclose = function() {...}, but it does not work when the user closes the tab

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2016-07-23
@iNeedHelp

You need to delete it not on the client side, but on the server side. Accordingly, on the server side, you also need to catch this event when the socket is disconnected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question