对于失败的websocket连接,我可以捕获“无法建立连接”错误吗?

时间:2014-04-07 17:53:17

标签: javascript websocket

我需要测试是否建立了与我的websocket服务器的连接。

此时,我可以连接到服务器,但我希望能够抓住该服务器无法访问的可能性,所以这个问题是关于无法建立websocket连接时或者什么时候该怎么做进行。

仅使用Firefox中的基本websocket代码,它将在大约20秒内超时并调用我的错误处理程序。但它也会抛出一个JavaScript错误(至少对我来说使用Firebug)会出现在浏览器中。然后日志显示:

Firefox can't establish a connection to the server at ws://192.168.0.1/.

到目前为止我已尝试过:

  • 通过添加我自己的window.timeout防止20秒超时,检查onopen处理程序是否已被调用,但这不会阻止JavaScript错误。
  • 在我自己的超时结束时强制关闭websocket,但现在我得到两个JavaScript错误 - 原来的加号:

    The connection to ws://192.168.0.1/ was interrupted while the page was loading.

  • 在我的代码中添加try {} catch(e) {},无论是在连接套接字还是关闭套接字时都没有变化。

有关如何让websocket错误无法在浏览器中显示的任何想法?

1 个答案:

答案 0 :(得分:7)

到目前为止我所学到的是:你不能:0(

...因为这是某种特定于浏览器的行为......

  • 所以你唯一能做的就是在ws对象上使用回调并祈祷......
  • 只是覆盖console.log; 0)

使用我所做的代码,但我删除了一些错误消息,也许它会帮助;)

例如:

  • Chrome不会抱怨死机,并默默尝试重新连接..
  • IE 11仍然提供脚本错误
  • 等。

包装WS的基本类:看看回调!

/**
 * Socket Class
 */
Client.Source.Network.Socket.Class = new Class({ implements: [Client.Source.Network.Socket.Interface] },function( Host, Port ){

var Configuration = {
    Server: {
        Protocol: 'ws',
        Host: Host,
        Port: Port
    },
    Event: {
        Open: function(){},
        Error: function(){},
        Message: function(){},
        Close: function(){}
    }
};

var Socket = null;

/**
 * @return {string}
 */
var HostUrl = function() {
    return Configuration.Server.Protocol + '://' + Configuration.Server.Host + ':' + Configuration.Server.Port + '/Connection'
};

/**
 * @returns {boolean}
 */
var IsSupported = function() {
    return "WebSocket" in window;
};

this.Open = function() {
    if( IsSupported() ) {
        Socket = new WebSocket( HostUrl() );
        Socket.onopen = Configuration.Event.Open;
        Socket.onerror = Configuration.Event.Error;
        Socket.onmessage = Configuration.Event.Message;
        Socket.onclose = Configuration.Event.Close;
    } else {

    }
};

this.Send = function( Text ) {
    Socket.send( Text );
};

this.Close = function() {
    Socket.close();
};

this.onOpen = function( Callback ) {
    Configuration.Event.Open = Callback;
};
this.onError = function( Callback ) {
    Configuration.Event.Error = Callback;
};
this.onMessage = function( Callback ) {
    Configuration.Event.Message = Callback;
};
this.onClose = function( Callback ) {
    Configuration.Event.Close = Callback;
};

});

看一下Connect():onError(),onClose()函数

/**
 * Network Class
 */
Client.Source.Network.Class = new Class({ implements: [Client.Source.Network.Interface] },function(){

var _Self = this;
var Socket = null;

this.Connect = function( Host, Port ) {
    Socket = new Client.Source.Network.Socket.Class( Host, Port );
    Socket.onOpen(function(){
        _Self.Gui().Create( Client.Module.Client.Gui() );
        Client.Module.Chat.Message('Connected', 'green', 11);
        Client.Module.Audio.Play( 'Client.Resource.Audio.Chat.UserOnline', 0.2 );
    });
    Socket.onMessage( Response );
    Socket.onError(function(){
        Client.Module.Chat.Message('Connection Error', 'red', 11);
    });
    Socket.onClose(function(){
        _Self.Gui().Destroy();
        Client.Module.Chat.Message('Disconnected', 'darkred', 11);
        Client.Module.Chat.Message('Connecting...', 'orange', 11);
        window.setTimeout(function () {
            _Self.Connect( Host, Port );
        }, 2000);
    });
    Socket.Open();
};

this.Request = function( Application, Request, Data ) {
    Socket.Send( JSON.stringify( { Application: Application, Request: Request, Data: Data } ) );
};
var Response = function( Message ) {
    Message = JSON.parse( Message.data );
    Library[Message.Application].Execute( Message.Request, Message.Data );
};

var Library = {};
this.Protocol = function( Application, Callback ) {
    Library[Application] = Callback;
};

var GuiObject = null;
this.Gui = function Gui() {
    if( GuiObject === null ) {
        GuiObject = new Client.Source.Network.Gui();
    }
    return GuiObject;
};
});
相关问题