禁止“与'xyz'的WebSocket连接失败" [英] Suppress "WebSocket connection to 'xyz' failed"

查看:177
本文介绍了禁止“与'xyz'的WebSocket连接失败"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个使用Web套接字的Web应用程序.这个想法是我的应用在启动时会尝试自动连接到最近连接到主机的主机.如果它无法与其中任何一个建立连接,则将用户定向到连接部分,并要求他们手动建立连接.

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.

所有这些工作.总而言之,我按顺序尝试每个已知主机,如果200毫秒后未连接(`readyState!= 1),它将尝试下一个主机.所有这些主机都应位于LAN上,因此200ms可以可靠地工作.如果列表中的最后一个也失败,则网络将打开一个模式,指示用户手动键入主机.

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.

问题是,通过尝试自动连接,我必须为尝试的主机创建websocket,这会向控制台输出如下错误消息:

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:

与'ws://lightmate:8080/'的WebSocket连接失败:错误 建立连接:net :: ERR_NAME_NOT_RESOLVED

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

与'ws://localhost:8080/'的WebSocket连接失败:错误 建立连接:net :: ERR_CONNECTION_REFUSED

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.

我试图通过用try/catch块包围对new WebSocket(address)的调用来删除它,但错误仍然存​​在,并且我还尝试设置了onerror处理程序,希望这样可以抑制错误消息.什么都没做.

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免责声明: 此问题类似于这个StackOverflow问题,但是这个问题已经过时一年了,共识是你做不到".我希望此后一切都变了.

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.

推荐答案

无法捕获该错误消息,该错误消息与创建WebSocket对象的代码异步发生.

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

此处有更多详细信息: Javascript在WebSocket实例化中未捕获错误

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

这篇关于禁止“与'xyz'的WebSocket连接失败"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆