如何终止 SignalR 连接?

Sni*_*ack 9 javascript c# asp.net-mvc signalr

我正在使用 SignalR 在网站上传输数据。但是 SignalR 应该只能发送一段时间的数据,如果时间段已过,则连接应该被终止。

$.connection.hub.stop()如果请求仍然未决且未完成,则停止功能被取消。但是无论发送了多少数据,都应该强制取消此请求。

我怎么能一个SignalR,连接?

Kir*_*512 6

正如您在有关超时和保持连接设置的 Microsoft 文档中所见,您可以在选项中定义 DisconnectTimeout。

例子:

protected void Application_Start(object sender, EventArgs e)
{
    // Make long-polling connections wait a maximum of 110 seconds for a
    // response. When that time expires, trigger a timeout command and
    // make the client reconnect.
    GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);

    // Wait a maximum of 30 seconds after a transport connection is lost
    // before raising the Disconnected event to terminate the SignalR connection.
    GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);

    // For transports other than long polling, send a keepalive packet every
    // 10 seconds. 
    // This value must be no more than 1/3 of the DisconnectTimeout value.
    GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10);

    RouteTable.Routes.MapHubs();
}
Run Code Online (Sandbox Code Playgroud)

编辑:由于CancellationToken无论如何您都想终止来自客户端的连接,因此您正在谈论一种行为,但不幸的是,SignalR 仍然不支持这点,正如您在此处此处所看到的,团队希望这样做,SignalR但仍然存在没有关于它的消息。