I need to test if the connection to my websocket server is established or not.
At this time, I CAN connect to the server, but I want to be able to catch the possibility of that server not being reachable, so this question is about what to do when the websocket connection cannot be established or when it times out.
Using just the basic websocket code in Firefox, it will timeout in about 20 seconds and call my error handler. But it will also throw a JavaScript error that (at least for me using Firebug) shows up in the browser. The log then shows:
Firefox can't establish a connection to the server at ws://192.168.0.1/.
What I've tried so far:
- Prevent the 20 second timeout by adding my own
window.timeout
that checks if theonopen
handler has been called yet or not, but that does not prevent the JavaScript error. Force-close the websocket at the end of my own timeout, but now I get TWO JavaScript errors - the original plus:
The connection to ws://192.168.0.1/ was interrupted while the page was loading.
Adding
try {} catch(e) {}
to my code, both when connecting the socket and closing it - no change.
Any ideas on how to get websocket errors to not show in the browser?