using Connected.Net.Messaging; using Microsoft.AspNetCore.SignalR; namespace Connected.Net.Hubs; public abstract class Server : IServer where THub : Hub { public event EventHandler? Received; protected Server(IHubContext hub) { Messages = new(); Clients = new(); Hub = hub; } public ClientMessages Messages { get; } public Clients Clients { get; } private IHubContext Hub { get; } public virtual async Task Send(TArgs args) { foreach (var client in Clients.Query()) { var message = new Message(client, args); Messages.Add(client.Connection, message); await Hub.Clients.Client(client.Connection).SendCoreAsync("Notify", new object[] { new MessageAcknowledgeArgs(message.Id), args }); } } public virtual async Task Receive(TArgs args) { Received?.Invoke(this, args); await Task.CompletedTask; } public async Task Acknowledge(string connection, IMessageAcknowledgeArgs args) { try { Messages.Remove(connection, args); await Task.CompletedTask; } catch (Exception ex) { await Hub.Clients.AllExcept(connection).SendCoreAsync("Exception", new object[] { new ServerExceptionArgs { Message = ex.Message } }); } } }