Reputation: 40
Javascript new Websocket(socketUrl)
runs on potentially forever if the server refuses to open the websocket, and by design, the surrounding script gets no information on why the server refuses to open it. I have seen client browsers sending websocket requests every few seconds for more than 10 weeks (I suppose a tab has been left open and unused for this whole time).
I have implemented a change in the surrounding client script that detects (via a normal http request) when the websocket requests have become useless, and then closes the socket. From this point onwards, clients who reload their javascript files won't cause problems anymore.
However, I wonder if there is a way to definitely deal with those forgotten tabs in clients who don't ever reload, or if the only thing I can do is to basically treat them like malware by blocking excess requests in the load balancer (haproxy).
Upvotes: 0
Views: 455
Reputation: 4095
Lets say your websocket object is named, websocket_obj
. Like:
websocket_obj = new Websocket(url); // url You know it
Then, you can close the socket, when you get an error. So,
websocket_obj.onerror = function(){
Websocket.close();
}
Here is the mdn_refs.
Upvotes: 0