/// <summary> /// Starts broadcast /// </summary> /// <param name="webSocketHandler">websocket broadcast handler</param> /// <param name="cachedProcessesProvider">Container of processes infromation</param> public async Task InvokeAsync(IWebSocketHandler webSocketHandler, ICachedProcessesProvider cachedProcessesProvider, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { await webSocketHandler.SendMessageToSubscribedAsync(JsonSerializer.Serialize(cachedProcessesProvider.GetProcesses()), cancellationToken); await Task.Delay(1000); } }
public void Configure(IApplicationBuilder app, IHostApplicationLifetime hostApplicationLifetime, IWebSocketHandler websocketHandler, ICachedProcessesProvider processesProvider, IBroadcastService broadcast) { hostApplicationLifetime.ApplicationStopping.Register(OnShutdown); Task.Run(() => processesProvider.InvokeAsync(tokenSource.Token)); Task.Run(() => broadcast.InvokeAsync(websocketHandler, processesProvider, tokenSource.Token)); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseWebSockets(); app.MapWebSocketManager("/ws", tokenSource.Token); }
/// <summary> /// Constructor /// </summary> /// <param name="webSocketConnectionManager">Storage for sockets</param> /// <param name="provider"> Provider to access processes information</param> public WebSocketHandler(IConnectionManager webSocketConnectionManager, ICachedProcessesProvider provider) { _webSocketConnectionManager = webSocketConnectionManager; _provider = provider; }