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.
|
|
|
|
using Connected.Hosting.Workers;
|
|
|
|
|
using Connected.Net.Messaging;
|
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
|
|
namespace Connected.Net.Hubs;
|
|
|
|
|
|
|
|
|
|
public abstract class ServerWorker<TArgs, THub> : ScheduledWorker
|
|
|
|
|
where THub : Hub
|
|
|
|
|
{
|
|
|
|
|
protected ServerWorker(IServer<TArgs> server, IHubContext<THub> hub)
|
|
|
|
|
{
|
|
|
|
|
Server = server;
|
|
|
|
|
Hub = hub;
|
|
|
|
|
Timer = TimeSpan.FromMilliseconds(500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected IServer<TArgs> Server { get; }
|
|
|
|
|
private IHubContext<THub> 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;
|
|
|
|
|
}
|
|
|
|
|
}
|