Suppress "WebSocket connection to 'xyz' failed"
Asked Answered
W

1

16

I've written a web application that uses web-sockets. The idea is that my app tries to auto-connect to recently connected to hosts when it starts up. If it can't establish a connection to any of them, then it directs the user to the connection part and asks them to establish a connection manually.

All of this works. In summary, I try each known host in order, and if 200ms later it hasn't connected (`readyState != 1), it tries the next one. All these hosts should be on the LAN so 200ms works pretty reliably. If the last one on the list fails too, then the web opens up a modal directing the user to manually type in a host.

The problem is, by trying to auto-connect, I have to create websockets to my attempted hosts, which outputs error messages like the following to the console:

WebSocket connection to 'ws://lightmate:8080/' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED

WebSocket connection to 'ws://localhost:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

While not a fatal flaw by any means, it's unsightly and gets in the way of my debugging.

I've tried to remove it by surrounding the calls to new WebSocket(address) with a try/catch block, and the errors still get through, and I've also tried to set an onerror handler, hoping that would suppress the error messages. Nothing's worked.

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 this StackOverflow question, but that question is out of date by a year, and the consensus there was "you can't". I'm hoping things have changed since then.

Wildon answered 13/8, 2015 at 1:56 Comment(6)
did you override the onerror callback? can you post the code?Reverse
I've posted the codeWildon
If you really mean WebSocket which is a natively supported class, it's obviously differently supported in all browsers. And API doesn't provide such configuration. Definitely you can't catch the exception because operation itself is asynchronous. What you could do was switch to any js library (ex.: socket.io), where verbosity levels and timeouts can be controlled. But I don't think comfortable debugging is worth this effort. Perhaps you might find another reason to have same implementation across all clientsLunnete
After trying a number of ideas, I'm starting to think the only way to avoid the messages is by disabling the logging (I have no idea how) or by clearing the log every time using the onclose callback - which is a terrible thought... Than another thought occurred to me... why can't you use the same address and use a load balancer like most apps do?Reverse
The problem with that strategy is that I'm not trying to access the internet. I'm trying to access a LAN which should have a special "lightmate" host configured, if that doesn't work trying "localhost", and then fall back to manual.Wildon
Chances are I'll just have to live with the errors.Wildon
T
2

There is no way to trap that error message, which occurs asynchronously to the code where the WebSocket object is created.

More details here: Javascript doesn't catch error in WebSocket instantiation

Throughout answered 22/8, 2018 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.