Web socket readyState in local server changes when connect to WIFI (in Firefox but not Chrome)?

I’m using Tcl as a local server and opening a web browser programmatically to serve as the user interface; and communicating over a web socket.

Everything works fine until I either connect to or disconnect from the external internet (which has nothing at all to do with the local application). At that time, no errors are thrown by the browser until a new request of the server is made over the web socket. The request reaches the server but the browser then closes the web socket. Any HTTP requests (in the same web page) continue to be made and received without error.

I check the readyState of the socket before making the request and it is 1. But it appears that after the browser sends the request it closes the socket. The error is:
The connection to ws://127.0.0.15:34405/reqWS/1/basic/main was interrupted while the page was loading. That would appear to indicate that the response from the server was in error; but that does not appear to be the issue for two reasons.

  1. These are the exact same requests as made before the non-local internet connection was opened or closed. (That’s just the connection itself; not attempting to load any pages.)
  2. It only happens in Firefox, not Chrome based browsers. I tested it in Brave and I can connect to and disconnect from the outside internet repeatedly and the web socket continues to work without issue.

The Tcl to open the browsers are provided below; the main reason for the differences is that Firefox no longer has an app mode. I used the userChrome.css to modify the appearance to make it look about the same, but don’t think that could have anything to do with the web socket.

# Firefox
exec firefox -P "ToolTest" -no-remote -new-instance -offline \
    -private -url http://${addr}:${port}/login &

# Brave
exec /usr/lib/brave-browser/brave --args --new-window\
    --window-size=3000,1800 --app=http://${addr}:${port}/login &

It might be understandable that Firefox might choose to close the local connection when the internet connection is opened or closed, as if clearing out old informaction; since local connections are not its main concern. However, what is confusing is that it PINGS the local server once when disconnecting the internet and three times when connecting, the PONGS are returned and the browser throws no errors. Also, the onerror and onclose handlers for the web socket do not fire at that time. And the next request is sent indicating that the readyState must have been a 1. Only, then, do the onerror and onclose handlers run and show a readyState of 3.

My question is, Do you know why this happens? Is it my error or just the way it is? Can I do anything to stop it happening?

While writing this, it occurs to me that I used to be able to hibernate the machine with the lcoal server still active and the application open in the browser; and, upon starting up again, the web socket still functioned. I think this may have started after changing the local address to one in which Tcl chooses a random(?) unused port rather than just hard coding an 8000 or 8080. You can see in the above error message that, in this case, the port was 34405.

Working in Manjaro Linux KDE.

Thank you for any guidance you may be able to provide.

  • 127.0.0.15 within spec, but very unusual.

    – 

  • 1

    @erik258 Thanks. I tried changing that to 127.0.0.1 or 127.000.000.001 and the socket is still closed. However, hard coding the port to 8000 results in the socket remaining open, even with 127.0.0.15. The reason I used that value and had Tcl choose a random unused port was to try to avoid conflicts with any other applications that might be using the same. Is there a better way to avoid conflicts? Thank you.

    – 




Leave a Comment