You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
56 lines
1.2 KiB
using Connected.Net.Messaging;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
namespace Connected.Net.Hubs;
|
|
|
|
public abstract class Server<THub, TArgs> : IServer<TArgs>
|
|
where THub : Hub
|
|
{
|
|
public event EventHandler<TArgs>? Received;
|
|
protected Server(IHubContext<THub> hub)
|
|
{
|
|
Messages = new();
|
|
Clients = new();
|
|
|
|
Hub = hub;
|
|
}
|
|
|
|
public ClientMessages<TArgs> Messages { get; }
|
|
|
|
public Clients<TArgs> Clients { get; }
|
|
private IHubContext<THub> Hub { get; }
|
|
|
|
public virtual async Task Send(TArgs args)
|
|
{
|
|
foreach (var client in Clients.Query())
|
|
{
|
|
var message = new Message<TArgs>(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 } });
|
|
}
|
|
}
|
|
}
|