public async Task <bool> ConnectNotificationsAsync(Func <FilePublishedNotification, Task> filePublishedHandler) { if (LoggedInUser == null) { return(false); } try { var hr = await apiService.NotificationsConnectSignalR(new SignalRHubConnectNotificationRequest { }, authToken.ToString(), userId.ToString()); if (string.IsNullOrWhiteSpace(hr.AccessToken) || string.IsNullOrWhiteSpace(hr.Url)) { return(false); } hub = new HubConnectionBuilder().WithUrl(hr.Url, options => { options.AccessTokenProvider = () => { return(Task.FromResult(hr.AccessToken)); }; }) .Build(); hub.On <byte[]>("OnFilePublished", b => { var n = FilePublishedNotification.FromBytes(b); if (n != null) { _ = filePublishedHandler(n); } }); await hub.StartAsync(); return(true); } catch { try { await hub.DisposeAsync(); } catch { } finally { hub = null; } } return(false); }
static async Task OnFilePublishedAsync(FilePublishedNotification fpn) { filesPublishedNotifications.Enqueue(fpn); await Task.CompletedTask; }