using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; namespace ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs; /// /// SignalR hub that pushes a fresh on every /// snapshot refresh. New connections receive the current snapshot /// immediately via ; subsequent refreshes are /// broadcast by . /// /// /// Connections are counted into /// so can stop building and broadcasting /// snapshots while nobody is watching. /// [Authorize(Policy = DashboardAuthenticationDefaults.HubClientsPolicy)] public sealed class DashboardSnapshotHub( IDashboardSnapshotService snapshotService, DashboardSnapshotHubConnectionCounter connectionCounter) : Hub { /// Method name used to push snapshot updates to clients. public const string SnapshotMessage = "SnapshotUpdated"; /// public override async Task OnConnectedAsync() { // Count the viewer before seeding it, so the publisher resumes its tick // no later than the first snapshot this connection renders. connectionCounter.Increment(); await Clients.Caller.SendAsync(SnapshotMessage, snapshotService.GetSnapshot()).ConfigureAwait(false); await base.OnConnectedAsync().ConfigureAwait(false); } /// public override async Task OnDisconnectedAsync(Exception? exception) { connectionCounter.Decrement(); await base.OnDisconnectedAsync(exception).ConfigureAwait(false); } }