好的,所以我有这个代码
var c = GlobalHost.ConnectionManager.GetHubContext<SomeHubClass>().Clients;
Run Code Online (Sandbox Code Playgroud)
现在,客户端返回一个IHubConext,它具有包含IGroupManager组的IHubConnectionContext.现在有没有从这里得到所有的组名?这甚至可以通过signalR接口实现,还是我必须自己跟踪每个集线器的所有组?
Dav*_*d L 13
SignalR没有公开的API来管理整个组,迭代组,甚至获取组的摘要列表.您只能添加或删除组.如果要保留组名列表,可以使用SomeHubClass的单例模式.List<string>在您可以轻松访问的单例中保留一个组名,甚至Dictionary<string, HashSet<string>>可以映射连接ID的名称和哈希集,尽管在这个实例中这可能有点过分.
请参阅http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-server#callfromoutsidehub以实现集线器的单例.
小智 6
您实际上可以像我一样使用反射获取所有组名称(因为我们需要的所有字段都是私有的),这也是我挖掘它的方法: IGroupManager -> _lifetimeManager -> _groups -> _groups
IGroupManager groupManager = signalRHubContext.Groups;
object lifetimeManager = groupManager.GetType().GetRuntimeFields()
.Single(fi => fi.Name == "_lifetimeManager")
.GetValue(groupManager);
object groupsObject = lifetimeManager.GetType().GetRuntimeFields()
.Single(fi => fi.Name == "_groups")
.GetValue(lifetimeManager);
IDictionary groupsDictionary = groupsObject.GetType().GetRuntimeFields()
.Single(fi => fi.Name == "_groups")
.GetValue(groupsObject) as IDictionary;
List<string> groupNames = groupsDictionary.Keys.Cast<string>().ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11434 次 |
| 最近记录: |