gpt4 book ai didi

c# - SignalR:客户端断开连接

转载 作者:太空狗 更新时间:2023-10-29 22:12:37 24 4
gpt4 key购买 nike

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();
}
}

关于c# - SignalR:客户端断开连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com