如何检测signalR连接是否被故意关闭



每当signalR连接因网络问题或故意丢失时,我都会尝试检测。我看到了很多解决方案,但它们似乎是针对旧版本的Aspnet核心signalR。(它们使用与参数stopcalled连接的条件(。在我的版本中,我只有condisconnectedsync,参数Exception Exception始终为null。

我在服务器端使用的是Microsoft.AspNet.SignalR.Core的2.4.1版本。以及"@aspnet/signer":"1.1.4";在客户端。

这是我的代码:

public override async Task OnDisconnectedAsync(Exception exception)
{

var httpContext = Context.GetHttpContext();
//get the user that lost connection or is being disconnected.
var user = users.SingleOrDefault(u => u.ConnectionId == Context.ConnectionId);
if(user != null && /* if connection was closed deliberately*/ )
{
//remove user from the list
users.Remove(user);
//log the user out.
ParticipantController.Logout(user.UserId);
}
await base.OnDisconnectedAsync(exception);
}

有人知道如何实现这一功能吗?我也想知道刷新页面是否也算作故意断开连接,如果是这样,我如何将其与实际的选项卡关闭或窗口关闭断开连接区分开来。

这是Signalr for.NET Core中的一个错误/不足。这里正在跟踪它:https://github.com/dotnet/aspnetcore/issues/26701.

最新更新