Skip to content Skip to sidebar Skip to footer

Can I Catch The "can't Establish A Connection" Error For A Failed Websocket Connection?

I need to test if the connection to my websocket server is established or not. At this time, I CAN connect to the server, but I want to be able to catch the possibility of that ser

Solution 1:

What i've learnd so far is: you cant :0(

... because this is somewhat browser specific behavior...

  • so the only thing you can do is using callbacks on ws object and pray ...
  • OR just overwrite console.log ;0)

with the code i did however i got rid of some error messages, maybe it'll help ;)

e.g:

  • Chrome won't complain about dead servers and silently try to reconnect..
  • IE 11 still gives script error
  • etc..

A Basic Class to wrap WS : Have a look at callbacks !

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

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

varSocket = null;

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

/**
 * @returns {boolean}
 */varIsSupported = function() {
    return"WebSocket"inwindow;
};

this.Open = function() {
    if( IsSupported() ) {
        Socket = newWebSocket( 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;
};

});

Have a look at the Connect() : onError(), onClose() function

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

var _Self = this;
varSocket = null;

this.Connect = function( Host, Port ) {
    Socket = newClient.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 } ) );
};
varResponse = function( Message ) {
    Message = JSON.parse( Message.data );
    Library[Message.Application].Execute( Message.Request, Message.Data );
};

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

varGuiObject = null;
this.Gui = functionGui() {
    if( GuiObject === null ) {
        GuiObject = newClient.Source.Network.Gui();
    }
    returnGuiObject;
};
});

Post a Comment for "Can I Catch The "can't Establish A Connection" Error For A Failed Websocket Connection?"