SignalR 1.0 beta connection factory

2019-01-12 07:08发布

Now that the new version of SignalR has done away with the IConnectionFactory interface, what is the recommended pattern to use to set the connection id of a client?

标签: signalr
1条回答
走好不送
2楼-- · 2019-01-12 07:58

There is no longer any recommended pattern for setting the connection id of a SignalR client in 1.0.

The best practice is to maintain state on your SignalR server that maps your application's users to a connection id. The obvious place to add connection ids to this map is in OnConnected. You can use OnDisconnected to remove connection ids.

Another option, which could also be done in OnConnected, is to add the client's connection id to a group with the name you would have created for the client in IConnectionIdFactory.CreateConnectionId before. Naturally, that group would only contain the one client, so you can send all the messages you would have sent to the custom connection id to the group instead.

Note: If you go with the mapping option you might also attempt adding connection ids to the map in OnReconnected if you are concerned about AppDomain restarts and you are storing this state in some sort of static variable instead of something more persistent. Obviously you would need shared state for this if you are scaling out SignalR.

查看更多
登录 后发表回答