HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了html5 – 如何捕获:WebSocket连接到’ws://失败:连接建立错误:net :: ERR_CONNECTION_REFUSED大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有简单的网络套接html5,当服务器启动时,每件事情都工作正常
问题是当我关闭服务器(用于测试)
我越来越 :

WebSocket connection to 'ws://127.0.0.1:7777/api' Failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

我无法抓住它永远不会跳到错误关闭错误的情况下

init: function () {
           this.m_wsiSendBinary = new WebSocket("ws://127.0.0.1:7681/wsapi");

           this.m_wsiSendBinary.onopen = function(evt) {
               cc.log("Send Binary WS was opened.");
           };

           this.m_wsiSendBinary.onmessage = (function(evt) {


               this.handleServerResponse(yStr);


           this.m_wsiSendBinary.onerror = function(evt) {

           };

           this.m_wsiSendBinary.onclose = function(evt) {
               cc.log("m_wsiSendBinary websocket instance closed.");
               self.m_wsiSendBinary = null;
           };

}).bind(this);

解决方法

我没有完整的答案,但是我处理了类似的问题并且有一个部分而不是那么优雅的解决方案(但可以帮助某人).不幸的是,没有消除错误消息.

两项业务要求:

> BR1 – 当服务器不可用时处理初始化状态.
> BR2 – 服务器停止时的处理状态.

BR1的解决方

var global_connection_openned=null;//Here you have the result
init: function () {
       global_connection_openned=false;
       this.m_wsiSendBinary = new WebSocket("ws://127.0.0.1:7681/wsapi");
       this.m_wsiSendBinary.onopen = function(evt)
          {
             global_connection_openned=true;
          };

BR2解决方案(假设为BR1)

//somewhere in your project called by seTinterval(..) which will detect the connection is lost (and tries to reestablish/reopen the connetion.
{
  if (this.m_wsiSendBinary==null || this.m_wsiSendBinary.readyState==3)
    this.init();

  if (!global_connection_openned)
    this.m_wsiSendBinary=null;
}

无论如何,如果有这个用例的坚实和正确的解决方案,我真的很好奇.

大佬总结

以上是大佬教程为你收集整理的html5 – 如何捕获:WebSocket连接到’ws://失败:连接建立错误:net :: ERR_CONNECTION_REFUSED全部内容,希望文章能够帮你解决html5 – 如何捕获:WebSocket连接到’ws://失败:连接建立错误:net :: ERR_CONNECTION_REFUSED所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。