Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signalr - endless $.connection.hub.disconnected event?

I'm using Signalr 2.2.1 with a successful websocket connection.

enter image description here

Here are the events for different states : ( simplified for brevity)

var hub = $.connection.moveShapeHub;


$.connection.hub.start().done(function ()
{
    console.log("hub started successfully");

}).fail(function () { console.log('Could not Connect!'); });


$.connection.hub.disconnected(function ()
{
    $.connection.hub.start();
    console.log('Connection disconnected')
});

My app is working fine as expected.

But look what happen when I disable the network card ( I access my computer not via localhost but via dynamic dns which goes to the world and then comes back to my computer)

At first you can see websocket connection error (I see it multiple times)

WebSocket connection to 'ws://xxxxxx.ddns.net/signalr/reconnect?transport=webSockets&messageId=d-C68A95E5-g%2C1&clientProtocol=1.5&connectionToken=%2FDJL8eAtVtSA3XKeap4Js3IrbkCm56C%2FWKCQtApGiMroWAgnzNoRHmJ0Y2LpIdWWWL%2BfY3dXvJqYHFfby1XYii0ibPpKM55PQuZyf9aH4k9JHIT79lWoMWBasIpa9Gjk&connectionData=%5B%5D&tid=2' failed: Error in connection establishment: net::ERR_INTERNET_DISCONNECTED

And then you see endless calls(!!!) to the negotiate

http://xxxx.ddns.net/signalr/negotiate?clientProtocol=1.5&connectionToken=%2FDJL8eAtVtSA3XKeap4Js3IrbkCm56C%2FWKCQtApGiMroWAgnzNoRHmJ0Y2LpIdWWWL%2BfY3dXvJqYHFfby1XYii0ibPpKM55PQuZyf9aH4k9JHIT79lWoMWBasIpa9Gjk&connectionData=%5B%7B%22name%22%3A%22moveshapehub%22%7D%5D&_=1485811277855

Wait ~15 seconds to see the endless loop :

enter image description here

Question

How can I fix those endless calls ? Or alternatvly - increase delay in those "negotiate calls" -say every 2 seconds ( instead of blazing fast endlessly 0.1 seconds)

Edit

I've changed this code :

$.connection.hub.disconnected(function ()
    {
        $.connection.hub.start();
        console.log('Connection disconnected')
    });

to this (remove hub start):

$.connection.hub.disconnected(function ()
    {
             console.log('Connection disconnected')
    });

And now I see only this message :

enter image description here

But now I'm losing all the basic idea of "trying restart connecting" in case of disconnect. So I ask again is there any reasonable solution or at least trying "restart the connection every 2 seconds" ?

like image 669
Royi Namir Avatar asked Jan 30 '17 21:01

Royi Namir


1 Answers

negotiate is the first request a SignalR client sends to establish a connection. You are trying to start the connection as soon as it gets disconnected in the disconnected event handler. Because the network is down negotiate fails and the disconnected event is invoked and you try to start the connection again. The documentation shows how to do it with the timeout:

$.connection.hub.disconnected(function() {
   setTimeout(function() {
       $.connection.hub.start();
   }, 5000); // Restart connection after 5 seconds.
});
like image 156
Pawel Avatar answered Nov 15 '22 04:11

Pawel