SignalR:客户端断开
问题描述:
SignalR如何处理客户端断开连接?如果我陈述如下,我是对的吗?SignalR:客户端断开
- SignalR将通过Javascript事件处理检测浏览器页面关闭/刷新,并将相应的数据包发送到服务器(通过持久连接);
- SignalR不会检测浏览器关闭/网络故障(可能只有超时)。
我的目标是长轮询运输。
我知道this question但我想说清楚一点。
答
如果用户刷新页面,则将其视为新连接。你是正确的断开是基于超时。
您可以通过执行
SignalR.Hubs.IConnected
和
SignalR.Hubs.IDisconnect
来处理集线器中的连接/重新连接和断开连接事件。
上面提到SignalR 0.5.x.
从the official documentation(目前为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 1.0中,SignalR.Hubs.IConnected和SignalR.Hubs.IDisconnect不再执行,而现在它只是在一个覆盖集线器本身:
public class Chat : Hub
{
public override Task OnConnected()
{
return base.OnConnected();
}
public override Task OnDisconnected()
{
return base.OnDisconnected();
}
public override Task OnReconnected()
{
return base.OnReconnected();
}
}