将1.x的onDisconnected()方法处理为SignalR 2.1.2

Ana*_*kar 1 c# json.net asp.net-mvc-4 signalr signalr-hub

我之前在Asp.Net-MVC项目中实现了信号R 1.1.3版本,但现在我需要用最新的信号更新信号R版本,它是信号R 2.1.2,在信号R 2.1.2里面问题是它onDisconnected()在hub类中不支持方法.我可以在项目中处理信号R的断开事件.

Dav*_*d L 6

在版本2.x中,Connection事件返回任务,其输入参数为bool stopCalled.您只需要更新您的方法以返回由base.OnDisconnected(stopCalled)返回的任务.

文档

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);
}
Run Code Online (Sandbox Code Playgroud)

编辑

我相信当前的SignalR文档实际上可能错误地建议您在没有bool stopCalled参数的情况下使用OnDisconnected().但是,查看HubBase的源代码(Hub继承自),您可以在2.x中找到声明为以下内容的OnDisconnected方法.

/// <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;
}
Run Code Online (Sandbox Code Playgroud)