Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR 2.0 Timeout Connection

I am using SignalR.But there is a problem about timeout.

Timeout disappears after a few minutes and does not work.

How can i set timeout Connection in SignalR 2.0 ?

like image 855
user3257373 Avatar asked Feb 01 '14 09:02

user3257373


People also ask

How do I set SignalR connection timeout?

Timeout configuration for SignalR can be set in Application_Start method of Global class in Global. asax. cs file. // Wait a maximum of 30 minutes after a transport connection is lost // before raising the Disconnected event to terminate the SignalR connection.

How long does a SignalR connection last?

The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command. Most of the time, such attempts will fail, but in some circumstances they might succeed.

Is SignalR real-time?

SignalR is fast and scalable Like the rest of ASP.NET, SignalR was built for high performance and is one of the fastest real-time frameworks around. Scale out across servers with built-in support for using Redis, SQL Server, or Azure Service Bus to coordinate messages between each instance.


1 Answers

You can use the following configuration in your Owin Startup class.

        // Make long polling connections wait a maximum of 110 seconds for a
        // response. When that time expires, trigger a timeout command and
        // make the client reconnect.
        GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(40);
        // Wait a maximum of 30 seconds after a transport connection is lost
        // before raising the Disconnected event to terminate the SignalR connection.
        GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);
        // For transports other than long polling, send a keepalive packet every
        // 10 seconds. 
        // This value must be no more than 1/3 of the DisconnectTimeout value.
        GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);
        //Setting up the message buffer size
        GlobalHost.Configuration.DefaultMessageBufferSize = 500;

Also when you want to keep your client connected to server all the time, you can try to connect it in disconnect hub event.

var tryingToReconnect = false;
$.connection.hub.disconnected(function () {
                //TODO: write the logic to reconnect to server.
                if(!tryingToReconnect) {
                    // notifyclient about disconnection
                    setTimeout(function() {
                        $.connection.hub.start();
                    }, 5000); // Restart connection after 5 seconds.
                }
            });
           $.connection.hub.reconnecting(function() {
                tryingToReconnect = true;
                console.log("reconnecting...");
            });
            $.connection.hub.reconnected(function() {
                tryingToReconnect = false;
                console.log("Reconnected");
            });
like image 56
UmarKashmiri Avatar answered Nov 17 '22 04:11

UmarKashmiri