public async Task <IActionResult> SubscribeUpdates(CancellationToken cancellationToken) { if (!HttpContext.WebSockets.IsWebSocketRequest) { return(BadRequest()); } var websocket = await HttpContext.WebSockets.AcceptWebSocketAsync(); var userId = _userManager.GetUserId(User); var websocketHelper = new WebSocketHelper(websocket); IEventAggregatorSubscription subscription = null; try { subscription = _eventAggregator.Subscribe <UserNotificationsUpdatedEvent>(async evt => { if (evt.UserId == userId) { await websocketHelper.Send("update"); } }); while (!cancellationToken.IsCancellationRequested) { await Task.Delay(2000, cancellationToken); } } finally { subscription?.Dispose(); await websocketHelper.DisposeAsync(CancellationToken.None); } return(new EmptyResult()); }
public ChannelSubscription(Channel <T> evts, IEventAggregatorSubscription innerSubscription, Func <T, Task> act, Logs logs) { _evts = evts; _innerSubscription = innerSubscription; _act = act; _logs = logs; _ = Listen(); }