Can I catch the "cannot connect" error for a failed connection to the web server?

I need to check if a connection to my websocket server is established or not.

Currently, I CAN connect to the server, but I want it to be able to catch the possibility that this server is unavailable, so this question is about what to do if the web socket connection cannot be established or when it is out of time.

Using only the websocket base code in Firefox, it will close in 20 seconds and will call my error handler. But this will also lead to a JavaScript error, which (at least for me using Firebug) appears in the browser. Then in the log is displayed:

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

What I have tried so far:

  • Prevent a 20 second timeout by adding my own window.timeout , which checks if the onopen handler onopen been called yet or not, but that doesn't interfere with the JavaScript error.
  • Force close the websocket at the end of my own timeout, but now I get TWO JavaScript errors - the original plus:

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

  • Adding try {} catch(e) {} to my code, both when the socket is connected and when it is closed, is unchanged.

Any ideas on how to make websocket errors not appear in browser?

+13
source share
1 answer

So far I have understood: you cannot: 0 (

... because this is browser-specific behavior ...

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

with the code I did, however I got rid of some error messages, maybe this will help;)

eg:

  • Chrome will not complain about dead servers and quietly try connecting again.
  • IE 11 still gives a script error
  • etc..

The main class for porting WS: look at callbacks!

 /** * 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; }; }); 

Look at the Connect () function: 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; }; }); 
+7
source

All Articles