I wrote a web application that uses web sockets. The idea is that my application tries to automatically connect to a newly connected host when it starts. If he cannot establish a connection with any of them, he directs the user to the connecting part and asks to establish a connection manually.
It all works. In general, I try every known host in order, and if after 200 ms it does not connect (`readyState! = 1), it tries to do the following. All these hosts must be on the local network, so 200 ms works quite reliably. If the last one from the list also does not work, the website opens a modal instruction to the user to manually enter the host.
The problem is that in trying to automatically connect, I need to create web files for my hosts trying to display error messages on the console like the following:
Connection to WebSocket with 'ws: // lightmate: 8080 /' failed: connection error: net :: ERR_NAME_NOT_RESOLVED
Connection to WebSocket with 'ws: // localhost: 8080 /' failed: connection error: network :: ERR_CONNECTION_REFUSED
Although this is not a fatal error in any way, it is unsightly and interferes with my debugging.
I tried removing it by surrounding the new WebSocket(address) calls with a try / catch block, and the errors still go onerror , and I also tried setting the onerror handler, hoping that this would suppress the error messages. Nothing succeeded.
connect: function(){ var fulladdr = completeServerAddress(address); try { connection = new WebSocket(fulladdr); connection.suppressErrorsBecauseOfAutoConnection = suppressErrorsBecauseOfAutoConnection; //Store this module-scoped variable in connection, so if the module changes suppression state, this connection won't. } catch (e){ //Make sure we don't try to send anything down this dead websocket connection = false; return false; } connection.binaryType = "arraybuffer"; connection.onerror = function(){ if (connection !== false && !connection.suppressErrorsBecauseOfAutoConnection){ Announce.announceMessage("Connection failed with server"); } connection = false; }; connection.onmessage = function(m){ rxMessage(ConnectionProtocol.BaseMessage.parseChunk(m.data)); }; connection.onclose = function(){ hooks.swing("disconnected", "", 0); if (connection !== false && !connection.suppressErrorsBecauseOfAutoConnection){ Announce.announceMessage("Connection lost with server"); } }; connection.onopen = function(){ sendMessages(ConnectionProtocol.HandshakeMessage.create(name, sources, sinks)); while (idlingmessages.length){ websocketConnection.send(idlingmessages.splice(0,1)[0]); } hooks.swing("connected", "", 0); }; },
Dupl Disclaimer: This question is similar to https://stackoverflow.com/a/2126269/2129 , but this question has been outdated for a year and the consensus was βyou cannotβ. I hope that everything has changed since then.