Files
ClientApiPoC/Shared/SignalR/TunnelServerBase.cs
2026-03-13 10:23:47 +01:00

36 lines
1.3 KiB
C#

using Microsoft.AspNetCore.SignalR;
namespace ClientApiPoC.Shared.SignalR {
public abstract class TunnelServerBase {
protected IHubContext<TunnelHub> HubContext { get; private set; }
protected ClientTracker Clients { get; private set; }
public TunnelServerBase(IHubContext<TunnelHub> 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<string, ISingleClientProxy> GetAllClients() {
var results = new Dictionary<string, ISingleClientProxy>();
var allClientIds = this.Clients.CurrentClientIds;
foreach (var clientId in allClientIds) {
var client = TryGetClient(clientId);
if (client != null) results.Add(clientId, client);
}
return results;
}
}
}