Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets not closing on IE if closing handshake is never made

I've been implementing a WebSocket with JavaScript and I have this one problem:
The endpoint that my web-application is connected to doesn't send back a close control frame when I'm sending it one.

This isn't that bad because browsers close the WebSocket connection after a while.

But a few things to notice are:

  • Browsers do only allow a specific amount of WebSockets to be connected at the same time.

  • When refreshing the web-application a new WebSocket is created

This causes the problem on IE:
When refreshing the web-application more than 6 times, a WebSocket connection cannot be made.

It seems like IE doesn't "delete" the WebSockets if they haven't been closed cleanly. And what's odd is that the amount of web sockets never seems to decrease by refreshing or just by waiting.

Only by closing the browser window, or the tab resets the number of WebSockets to 0.


I've done some researching and this is what I've found out:

Browsers do only support a specific amount of WebSockets to be connected at the same time.

IE supports 6 websockets to be connected [link]

Chrome supports 255 websockets to be connected [link].

And socket.onclose() isn't triggered when you do socket.close(), it is called when the endpoint responses with a close message. [link]

IE waits 15 seconds for the endpoint to send the close message [link].

Chrome waits for 60s for the responding message [Sorry, no link for this, found this out by testing].

If no response message is received, the browser closes the WebSocket connection and a TimeoutError should occur.

Please correct me if I'm wrong :)


I've tried to use unbeforeload to disconnect from the endpoint in hope that the browser would close the connection after a while, but with no luck. [link].
It can also be the cause of that IE aren't able to do request inside the unbeforeload function [link].

Question:

  1. Is there any way to reset the number of WebSockets that are connected in the browser to the endpoint with JavaScript?
  2. Is there a way to disconnect a WebSocket from the endpoint immediately without closing the connection cleanly?
  3. Is the only way to get this to work to inform the ones who host their endpoint make some changes so they do send a closing frame back?
  4. Is there anything I've misunderstood or that I could try to get this to work?

Here is (in my opinion) good documentation about the WebSocket protocols if somebody would like to read more about it [link1] [link2].

UPDATE:

Only by refreshing the web-application on IE the WebSockets don't get destroyed.
If you navigate between pages in the web-application a new WebSocket will be made but the last WebSocket will get destroyed.

like image 390
OuuGiii Avatar asked Aug 17 '18 10:08

OuuGiii


People also ask

How do I close WebSocket connection in browser?

close() The WebSocket. close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED , this method does nothing.

Do we need to close WebSocket?

If you are writing a server, you should make sure to send a close frame when the server closes a client connection. The normal TCP socket close method can sometimes be slow and cause applications to think the connection is still open even when it's not.

How do I close a WebSocket manually?

send , ws. recv , or ws. close .

How long does a WebSocket stay open?

A WebSocket connection can in theory last forever. Assuming the endpoints remain up, one common reason why long-lived TCP connections eventually terminate is inactivity.


1 Answers

If it is just an edge case problem, then using a http fallback might be your only option. I guess you already do this for proxy servers that block socket connection away.

like image 157
David Bradshaw Avatar answered Oct 02 '22 21:10

David Bradshaw