asp.net mvc - SignalR - Detect disconnected client in no time -
i working on application needs recognize when log's off system.
i've set ping interval 5 seconds. have public override task onconnected()
, public override task ondisconnected()
, , triggered each time client connects , disconnects (i remember clients names , connections while connecting, , remove data list when disconnected)
so, when client log's off system, connection lost , doesn't send ping response. have solution working, when takes time, around 30 seconds run ondisconnected
function. there possibility make process faster? have detect in @ least 4-5 seconds.
edit: here code hub.cs:
public class signalrconnection { public string connectionid { get; set; } public string username { get; set; } } public static list<signalrconnection> connections = new list<signalrconnection>(); public override task onconnected() { connections.add(new signalrconnection() { connectionid = context.connectionid, username=context.user.identity.name}); return base.onconnected(); } public override task ondisconnected() { var fordelete = connections.firstordefault(p => p.connectionid == context.connectionid); connections.remove(fordelete); clients.all.logoffactions(fordelete.username); return base.ondisconnected(); }
global.asax
globalhost.configuration.connectiontimeout =timespan.fromseconds(6); globalhost.configuration.disconnecttimeout = timespan.fromseconds(6); globalhost.configuration.keepalive = timespan.fromseconds(2);
you can detect happens javascript. understanding happens connection: http://www.asp.net/signalr/overview/guide-to-the-api/handling-connection-lifetime-events
basically first, connection goes in reconnecting
state (can slow connection, short interruption in connection ... ) in state tries connect again. default 6 times every 5 seconds, takes 30 seconds. if no success fires disconnected
event going call hub's ondisconnected
method.
you can use reconnecting function in javascript, in state, client can still come back.
you can use js:
var tryingtoreconnect = false; $.connection.hub.reconnecting(function() { tryingtoreconnect = true; }); $.connection.hub.reconnected(function() { tryingtoreconnect = false; }); $.connection.hub.disconnected(function() { if(tryingtoreconnect) { notifyuserofdisconnect(); // function notify user. } });
when connection down $.connection.hub.reconnecting(function() {
called instantly, (but firing of event doesn't mean connection down! might quick connection problem)
another useful thing connectionslow
event.
$.connection.hub.connectionslow(function() { notifyuserofconnectionproblem(); // function notify user. });
most importantly understand states , transitions described in linked description.
i hope helps. cheers!
Comments
Post a Comment