using Connected.Hosting.Workers; using Connected.Net.Messaging; using Microsoft.AspNetCore.SignalR; namespace Connected.Net.Hubs; public abstract class ServerWorker : ScheduledWorker where THub : Hub { protected ServerWorker(IServer server, IHubContext hub) { Server = server; Hub = hub; Timer = TimeSpan.FromMilliseconds(500); } protected IServer Server { get; } private IHubContext Hub { get; } protected override async Task OnInvoke(CancellationToken cancellationToken) { await Send(cancellationToken); /* * Clean up every 15 seconds */ if (Count % 30 == 0) await Clean(cancellationToken); } private async Task Send(CancellationToken cancellationToken) { var messages = Server.Messages.Dequeue(); foreach (var item in messages) await Hub.Clients.Client(item.Client.Connection).SendCoreAsync("Notify", new object[] { new MessageAcknowledgeArgs(item.Id), item.Arguments }, cancellationToken); } private async Task Clean(CancellationToken cancellationToken) { Server.Messages.Clean(); Server.Clients.Clean(); await Task.CompletedTask; } }