Pergunta

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?

Foi útil?

Solução

Check out https://github.com/SignalR/SignalR/wiki/Configuring-SignalR , there are settings for :

DisconnectTimeout KeepAlive & Heatbeat interval

These could all be applied to help in maintaining your dictionary.

In my experience a graceful disconnect seems to work perfectly on signalR (still problems with win-apps) , if it ungracefully disconnects in a few minutes the connection will timeout and the disconnect method will fire and remove it from your dictionary like Drew said.

You could create a method that sends a message to all clients and log the returned connection ID and then remove any entries that are old, but in practice the disconnect method does work/work itself out, I would only implement the heartbeat interval if you really need to keep a very close eye on the connections.

Outras dicas

If it doesn't fire it's a bug and you should report an issue to the SignalR project on GitHub. Here's a list of open issues with Disconnects at this time.

Be aware that diff. transports have diff. disconnect detection logic and so, depending on which transport the user is using, you will see diff. patterns of when the Disconnect fires, but it SHOULD fire eventually for all transports.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top