using Microsoft.AspNetCore.SignalR; namespace ClientApiPoC.Shared.SignalR { public abstract class TunnelServerBase { protected IHubContext HubContext { get; private set; } protected ClientTracker Clients { get; private set; } public TunnelServerBase(IHubContext hubContext, ClientTracker clientTracker) { if (hubContext == null) throw new ArgumentNullException(nameof(hubContext)); if (clientTracker == null) throw new ArgumentNullException(nameof(clientTracker)); this.HubContext = hubContext; this.Clients = clientTracker; } protected ISingleClientProxy? TryGetClient(string clientId) { ISingleClientProxy? client; try { client = this.HubContext.Clients.Client(clientId); } catch { client = null; } return client; } protected IDictionary GetAllClients() { var results = new Dictionary(); var allClientIds = this.Clients.CurrentClientIds; foreach (var clientId in allClientIds) { var client = TryGetClient(clientId); if (client != null) results.Add(clientId, client); } return results; } } }