SignalR:客户端断开连接 [英] SignalR: client disconnection
问题描述
SignalR如何处理客户端断开连接?我声明以下内容对吗?
How does the SignalR handle client disconnection? Am I right if I state the following?
- SignalR将通过Javascript事件处理来检测浏览器页面的关闭/刷新,并将适当的数据包发送到服务器(通过持久连接);
- SignalR将不会检测到浏览器关闭/网络故障(可能仅通过超时).
我瞄准的是长轮询运输.
I aim the long-polling transport.
我知道这个问题,但想说明一点为我清除.
I'm aware of this question but would like to make it a bit clear for me.
推荐答案
如果用户刷新页面,则将其视为新连接.您是正确的,断开连接是基于超时的.
If a user refreshes the page, that is treated as a new connection. You are correct that the disconnect is based on a timeout.
您可以通过实现SignalR.Hubs.IConnected
和SignalR.Hubs.IDisconnect
来处理集线器中的Connect/Reconnect和Disconnect事件.
You can handle the Connect/Reconnect and Disconnect events in a Hub by implementing SignalR.Hubs.IConnected
and SignalR.Hubs.IDisconnect
.
以上指SignalR0.5.x.
The above referred to SignalR 0.5.x.
来自官方文档 (当前适用于v1.1.3):
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();
}
}
这篇关于SignalR:客户端断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!