Initial commit.
This commit is contained in:
36
Shared/SignalR/TunnelServerBase.cs
Normal file
36
Shared/SignalR/TunnelServerBase.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user