How to use SignalR events to keep connection alive in the right way?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • x0rn
    New Member
    • Feb 2014
    • 3

    How to use SignalR events to keep connection alive in the right way?

    I am developing real time client-server application using SignalR, ASP.NET and C#. I am using localhost as host and VS2103.

    My questions are:

    1. After I close server, on web-client the "Reconnect" event occurs and the "Disconnect" event occurs only after. Why?

    2. The "Disconnect " occurs 30+ seconds after of unknown "Reconnect" . How to reduce this time?

    3. I need the client to connect to the server on start. the "Reconnect" should occurs within fixed interval only. If "Reconnect" interval time is over and the client should connect as new client.

    And generally - How to keep alive connection using SignalR in the right way?

    I am using this code:

    C#
    Code:
            
    public override Task OnDisconnected()
            {
                clientList.RemoveAt(nIndex);
                Console.WriteLine("Disconnected {0}\n", Context.ConnectionId);
                return (base.OnDisconnected());
            }
    
            public override Task OnReconnected()
            {
                Console.WriteLine("Reconnected {0}\n", Context.ConnectionId);
                return (base.OnReconnected());
            }
    Javascript
    Code:
    $.connection.hub.reconnected(function () {
        // Html encode display name and message.
        var encodedName = $('<div />').text("heartbeat").html();
        var now = new Date();
    
        // Add the message to the page.
        $('#discussion').append('Reconnected to server: ' + now + '</br>');
    
    });
    
    $.connection.hub.disconnected(function () {
        // Html encode display name and message.
        var encodedName = $('<div />').text("heartbeat").html();
        var now = new Date();
        // Add the message to the page.
        $('#discussion').append('Disconnected from server: ' + now + '</br>');
    });
    After Connect output:
    Code:
    message received from server http://localhost:8089/signalr : Fri Feb 21 2014 10:53:02
    After Close Server output:
    Code:
    Reconnected to server: Fri Feb 21 2014 10:53:22 <-- Why, if i close server ???
    Disconnected from server: Fri Feb 21 2014 10:53:53 <-- Why 40-50 seconds after server close ?
    Last edited by Frinavale; Feb 26 '14, 08:21 PM. Reason: Fixed bold tags.
Working...