Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I catch a WebSocket connection interruption?

In Firefox (at least), if you hit ESC, then it will close all open WebSockets connections. I need to capture that disconnection and try to re-connect once it's available again.

Here's an example of the code I've tried to implement, but nothing I can figure out will catch the error and allow me to handle it gracefully.

Have a look at the code: http://jsfiddle.net/w5aAK/

var url = "ws://echo.websocket.org";
    try {
        socket = window['MozWebSocket'] ? new MozWebSocket(url) : new WebSocket(url);
        socket.onopen = function(){
            console.log('Socket is now open.');
        };
        socket.onerror = function (error) {
            console.error('There was an un-identified Web Socket error');
        };
        socket.onmessage = function (message) {
            console.info("Message: %o", message.data);
        };
    } catch (e) {
        console.error('Sorry, the web socket at "%s" is un-available', url);
    }

setTimeout(function(){
    socket.send("Hello World");
}, 1000);

Turn on your console and watch the output.

Am I doing something wrong here, or is it just not possible because the connection is running outside of the scope of the JS script?

Any input would be helpful.

Thanks!

like image 459
Zach Avatar asked Nov 29 '12 00:11

Zach


2 Answers

You can attach a handler to the socket.onclose event. It will be called when you hit ESC and the connection is interrupted.

See: http://jsfiddle.net/w5aAK/1/

One problem that you can't get around at the moment is the interrupted error being output to the console. There's no way of capturing that at the moment I'm afraid.

like image 192
leggetter Avatar answered Oct 02 '22 23:10

leggetter


You can't catch it and it's not your fault. It's FireFox bug. Vote for it here:

https://bugzilla.mozilla.org/show_bug.cgi?id=712329

I personally tried all kind of solutions:

event handlers onunload onbeforeunload onclose try..catch some js error handling 3rd party services etc.

You can log to console your socket, it's closed before unload, but FF thinks different.. :(

Solution (not answer directly to the answer, but it works):

It's a bug, so you can't catch, but this info is not Solution. After all kind of crazy workarounds and tries to catch that bug, i finally found this working. If you use socket.io to work with WebScokets it can work with different transport technologies. xhr-polling works with Firefox.

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Firefox/x.x     or Firefox x.x (ignoring remaining digits);
    socket = io.connect('//' + node_server + '/', {
        transports: ['polling']
    });
} else {
    socket = io.connect('//' + node_server + '/');
}

What helped me - might help you too:

Web Socket support in Node.js/Socket.io for older browser

Define transport types on the client side

socket.io doens't work with transports: [ 'xhr-polling' ]

like image 23
Lukas Liesis Avatar answered Oct 03 '22 01:10

Lukas Liesis