I'm using ASP.NET MVC 5 and using SignalR v2.2.2 (the latest). My application is working all okay until the page has been idle for a while (not sure exactly now long but between 5 to 15 mins idle). Once it has been idle for this period of time, the buttons on the web page no longer work (as I assume they need an active connection).
My preferred way to solve this is to be able to increase the timeout of the connection. How can I do this?
If this is not possible, how can I detect that the connection has been dropped on the page and/or how can I re-establish connection if required?
My web page uses javascript and the code that establishes the connection to the SignalR hub is this:
$.connection.hub.start()
.done(function () {
// Does stuff here...
})
.fail(function (e) { console.log(e); });
Thank you
There are 2 recipes you can implement:
1.Reconnect if the connection drops:
$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 5000);
});
2.Use the pingInterval
so you try to keep the connection alive:
$.connection.hub.start({ pingInterval: 6000 })
From Microsoft Docs ,
Handle the connectionSlow event to display a message as soon as SignalR is aware of connection problems, before it goes into reconnecting mode.
Handle the reconnecting event to display a message when SignalR is aware of a disconnection and is going into reconnecting mode.
Handle the disconnected event to display a message when an attempt to reconnect has timed out. In this scenario, the only way to re-establish a connection with the server again is to restart the SignalR connection by calling the Start method, which will create a new connection ID.
There's a much simpler way now: .withAutomaticReconnect()
Seethis post
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.