TN.*_*TN. 9 .net c# signalr signalr-hub
如何从SignalR获取打开的连接列表而不发送"ping"或手动将它们注册到自己的列表中?
更新: 或者只是为了获取从集线器发送消息的客户端数量.我想知道我应该等待多少响应(无需等待整个超时).
(因为,当从集线器调用客户端时,SignalR不支持返回值,我通过客户端发送到集线器的另一条消息收集结果.)
澄清:我假设SignalR必须知道哪些连接正在发送消息.
我还没有找到任何直接的方法来做到这一点。
到目前为止,我提出的最好的方法是遵循教程 - USERS BY CONNECTIONS IN SIGNALR,您可以在链接中找到更多代码,我已将其简化以供基本理解。
public void Register(HubClientPayload payload, string connectionId)
{
lock (_lock)
{
List<String> connections;
if (_registeredClients.TryGetValue(payload.UniqueID, out connections))
{
if (!connections.Any(connection => connectionID == connection))
{
connections.Add(connectionId);
}
}
else
{
_registeredClients[payload.UniqueID] = new List<string> { connectionId };
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
public Task Disconnect(string connectionId)
{
lock (_lock)
{
var connections = _registeredClients.FirstOrDefault(c => c.Value.Any(connection => connection == connectionId));
// if we are tracking a client with this connection remove it
if (!CollectionUtil.IsNullOrEmpty(connections.Value))
{
connections.Value.Remove(connectionId);
// if there are no connections for the client, remove the client from the tracking dictionary
if (CollectionUtil.IsNullOrEmpty(connections.Value))
{
_registeredClients.Remove(connections.Key);
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
还,
public Task Reconnect(string connectionId)
{
Context.Clients[connectionId].reRegister();
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1755 次 |
| 最近记录: |