2

I am using Signalr in an application I'm writing and storing all the user connections in a concurrent dictionary

ConcurrentDictionary<string, User> _users = new ConcurrentDictionary<string, User>();

e.g. https://github.com/SignalR/SignalR/blob/master/samples/SignalR.Hosting.AspNet.Samples/Hubs/ShapeShare/ShapeShare.cs

I have implemented the IDisconnect interface on my Hub and I'm removing users from the dictionary when they disconnect

I am wondering how reliable the Disconnect method really is? Does it capture all the different ways that a user could diconnect? I dont want the dictionary to grow and grow indefinitely

I was thinking of maybe having a timer to periodically traverse the dictionary and remove users who havent had any recent activity

Is this necessary? Can I rely on the disconnect method?

4

2 回答 2

1

查看https://github.com/SignalR/SignalR/wiki/Configuring-SignalR,有以下设置:

DisconnectTimeout KeepAlive 和 Heatbeat 间隔

这些都可以用来帮助维护您的字典。

根据我的经验,正常断开连接似乎在 signalR 上运行良好(win-apps 仍然存在问题),如果它在几分钟内不正常断开连接,则连接将超时,并且断开连接方法将触发并将其从字典中删除,就像 Drew 说的那样。

您可以创建一个向所有客户端发送消息并记录返回的连接 ID 的方法,然后删除任何旧的条目,但实际上断开方法确实可以工作/自行解决,如果您真的这样做,我只会实现心跳间隔需要密切关注连接。

于 2012-08-14T10:36:47.253 回答
1

如果它没有触发,那就是一个错误,您应该向 GitHub 上的 SignalR 项目报告问题这是目前断开连接的未解决问题列表

请注意差异。运输有差异。断开检测逻辑等,根据用户使用的传输方式,您将看到差异。Disconnect 何时触发的模式,但它最终应该为所有传输触发。

于 2012-08-09T16:41:32.413 回答