Anand Thakkar
Anand Thakkar

Reputation: 302

Handle onDisconnected() method of 1.x to SignalR 2.1.2

I had implemented signal R 1.1.3 version in Asp.Net-MVC project earlier, but now i need to update signal R version with the latest one and it is signal R 2.1.2, inside the signal R 2.1.2 the problem is that its not support onDisconnected() method in hub class. so can i handle Disconnection event of signal R in my project.

Upvotes: 1

Views: 2754

Answers (1)

David L
David L

Reputation: 33815

In version 2.x, Connection events return Task taking an input parameter of bool stopCalled. You simply need to update your method to return task, which is returned by base.OnDisconnected(stopCalled).

Documentation

public override Task OnDisconnected(bool stopCalled)
{
    // 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(stopCalled);
}

EDIT

I believe the current SignalR documentation may actually erroneously be advising you to use OnDisconnected() without a bool stopCalled parameter. However, looking at the source for HubBase (which Hub inherits from), you can find the OnDisconnected method declared as the following in 2.x.

/// <summary>
/// Called when a connection disconnects from this hub gracefully or due to a timeout.
/// </summary>
/// <param name="stopCalled">
/// true, if stop was called on the client closing the connection gracefully;
/// false, if the connection has been lost for longer than the
/// <see cref="Configuration.IConfigurationManager.DisconnectTimeout"/>.
/// Timeouts can be caused by clients reconnecting to another SignalR server in scaleout.
/// </param>
/// <returns>A <see cref="Task"/></returns>
public virtual Task OnDisconnected(bool stopCalled)
{
    return TaskAsyncHelper.Empty;
}

Upvotes: 6

Related Questions