How to determine server disconnection from SignalR client?
The provided answers will no longer work.
This is the new way to stop/disconnect connections.
myHub.stop().then(() => {
alert('Server has disconnected');
});
Detect on disconnect user signalr
Add this overriden method into your Hub
class:
public override Task OnDisconnected(bool stopCalled)
{
// do the logging here
Trace.WriteLine(Context.ConnectionId + ' - disconnected');
return base.OnDisconnected(stopCalled);
}
This will handle and log disconects on server side while I think it doesn't make much sense to track it on client
side as it's unlikely your SignalR session would be terminated from server side - see here.
Edit in regard to @stuartd
comment:
you could also override
public override Task OnReconnected()
{
Trace.WriteLine(Context.ConnectionId + ' - reconnected');
return base.OnReconnected();
}
so you'll be able to track cases when Client tried reconnect to Server and succeed.
Edit#2:
Then you can of course use same mechanism for tracking of OnConnected
event
public override Task OnConnected()
{
Trace.WriteLine(Context.ConnectionId + ' - reconnected');
return base.OnConnected();
}
So all tracking code is conveniently kept just on server-side which is more robust in cases when client is logged into hub but for some reason failed to execute method you're using to track logged state.
SignalR - Server side method to detect if a client disconnects from a hub?
SignalR has OnConnected, OnDisconnected, and OnReconnected that are called every time the client does one of those actions. You can simply override them:
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected()
{
//custom logic here
return base.OnDisconnected();
}
public override Task OnReconnected()
{
return base.OnReconnected();
}
I've found them to be extremely useful also for debugging purposes. If you're wanting to set a timer for each person, you should use some sort of connectionMapping along with the above functions to keep track of your users.
SignalR: client disconnection
If a user refreshes the page, that is treated as a new connection. You are correct that the disconnect is based on a timeout.
You can handle the Connect/Reconnect and Disconnect events in a Hub by implementing SignalR.Hubs.IConnected
and SignalR.Hubs.IDisconnect
.
The above referred to SignalR 0.5.x.
From the official documentation (currently for v1.1.3):
public class ContosoChatHub : Hub
{
public override Task OnConnected()
{
// Add your own code here.
// For example: in a chat application, record the association between
// the current connection ID and user name, and mark the user as online.
// After the code in this method completes, the client is informed that
// the connection is established; for example, in a JavaScript client,
// the start().done callback is executed.
return base.OnConnected();
}
public override Task OnDisconnected()
{
// Add your own code here.
// For example: in a chat application, mark the user as offline,
// delete the association between the current connection id and user name.
return base.OnDisconnected();
}
public override Task OnReconnected()
{
// Add your own code here.
// For example: in a chat application, you might have marked the
// user as offline after a period of inactivity; in that case
// mark the user as online again.
return base.OnReconnected();
}
}
Notify SignalR Server On Client Disconnected Accidentally (Disgracefully)
Since WebSocketTransport is not available at Xamarin, I advice to use "Ping" workaround.
- Implement
Ping()
method at server-side. - Call this method from clients periodically (say 1/2 of the timeout interval).
- Within this method save
ConnectionId
:DateTime
key/value pair to staticConcurrentDictionary
at server-side. - Run background Task at server-side and check
DateTime
for all dictionary keys. - Remove old-ones and call appropriate code.
Related Topics
How to Select Every 2Nd Element of an Array in a for Loop
Pass Node Js Variable to Ejs Template
How to Assign [(Ngmodel)] With an Empty/Null/Undefined Object - Angular 4
Reset Form Input Values in React
Angular:How to Implement Condtion Based Click Event
How to Trigger a Click Event on Disabled Elements
How to Get Line Break Within String Interpolation in Angularjs
React Component Not Re-Rendering When State Changes
How to Remove a Json Inside a Json by a Key Using Jquery
How to Tell Chrome Web Debugger to Show the Current Mouse Position in Page Coordinates
How to Load Content of Page Without Refreshing the Whole Page
Remove Duplicate Objects from Json Array
Reactjs, Table Cell With Different Background Color
Push Objects into an Array in Reactjs
Count the Number of Times a Same Value Appears in a JavaScript Array