Esempio n. 1
0
        /// <summary>
        /// Update other users with a message from SyncViewModel
        /// </summary>
        /// <param name="syncResultsList">SyncViewModel</param>
        /// <param name="type">optional debug name</param>
        /// <returns>Completed send of Socket SendToAllAsync</returns>
        private async Task SyncMessageToSocket(IEnumerable <SyncViewModel> syncResultsList, ApiNotificationType type = ApiNotificationType.Unknown)
        {
            var list = syncResultsList.Select(t => new FileIndexItem(t.FilePath)
            {
                Status = t.Status, IsDirectory = true
            }).ToList();

            var webSocketResponse = new ApiNotificationResponseModel <
                List <FileIndexItem> >(list, type);

            await _notificationQuery.AddNotification(webSocketResponse);

            await _connectionsService.SendToAllAsync(webSocketResponse, CancellationToken.None);
        }
Esempio n. 2
0
        public async Task NotificationToAllAsync <T>(ApiNotificationResponseModel <T> message,
                                                     CancellationToken cancellationToken)
        {
            await _webSocketConnectionsService.SendToAllAsync(message, cancellationToken);

            await _notificationQuery.AddNotification(message);
        }
        private async Task HeartbeatAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                await _webSocketConnectionsService.SendToAllAsync(HEARTBEAT_MESSAGE, cancellationToken);

                await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
            }
        }
Esempio n. 4
0
        private async Task HeartbeatAsync(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                var webSocketResponse =
                    new ApiNotificationResponseModel <HeartbeatModel>(new HeartbeatModel(SpeedInSeconds),
                                                                      ApiNotificationType.Heartbeat);
                await _connectionsService.SendToAllAsync(webSocketResponse, cancellationToken);

                await Task.Delay(TimeSpan.FromSeconds(SpeedInSeconds), cancellationToken);
            }
        }
Esempio n. 5
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            IChangefeed <ThreadStats> threadStatsChangefeed = await _threadStatsChangefeedDbService.GetThreadStatsChangefeedAsync(stoppingToken);

            while (!stoppingToken.IsCancellationRequested && (await threadStatsChangefeed.MoveNextAsync(stoppingToken)))
            {
                string newThreadStats = threadStatsChangefeed.CurrentNewValue.ToString();
                await Task.WhenAll(
                    _serverSentEventsService.SendEventAsync(newThreadStats),
                    _webSocketConnectionsService.SendToAllAsync(newThreadStats)
                    );
            }
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await _threadStatsChangefeedDbService.EnsureDatabaseCreatedAsync();

            IChangefeed <ThreadStats> threadStatsChangefeed = await _threadStatsChangefeedDbService.GetThreadStatsChangefeedAsync(stoppingToken);

            try
            {
                await foreach (ThreadStats threadStatsChange in threadStatsChangefeed.FetchFeed(stoppingToken))
                {
                    string newThreadStats = threadStatsChange.ToString();
                    await Task.WhenAll(
                        _serverSentEventsService.SendEventAsync(newThreadStats),
                        _webSocketConnectionsService.SendToAllAsync(newThreadStats)
                        );
                }
            }
            catch (OperationCanceledException)
            { }
        }
Esempio n. 7
0
        internal async Task WorkItem(string subPath, IStorage subPathStorage,
                                     IStorage thumbnailStorage)
        {
            try
            {
                _logger.LogInformation($"[ThumbnailGenerationController] start {subPath}");
                var thumbnail = new Thumbnail(subPathStorage,
                                              thumbnailStorage, _logger);
                var thumbs = await thumbnail.CreateThumb(subPath);

                var getAllFilesAsync = await _query.GetAllFilesAsync(subPath);

                var result = new List <FileIndexItem>();
                foreach (var item in
                         getAllFilesAsync.Where(item => thumbs.FirstOrDefault(p => p.Item1 == item.FilePath).Item2))
                {
                    if (item.Tags.Contains("!delete!"))
                    {
                        continue;
                    }

                    item.SetLastEdited();
                    result.Add(item);
                }

                if (!result.Any())
                {
                    return;
                }

                var webSocketResponse =
                    new ApiNotificationResponseModel <List <FileIndexItem> >(result, ApiNotificationType.ThumbnailGeneration);
                await _connectionsService.SendToAllAsync(webSocketResponse, CancellationToken.None);

                _logger.LogInformation($"[ThumbnailGenerationController] done {subPath}");
            }
            catch (UnauthorizedAccessException e)
            {
                _logger.LogError($"[ThumbnailGenerationController] catch-ed exception {e.Message}", e);
            }
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            ITextWebSocketSubprotocol textWebSocketSubprotocol = new PlainTextWebSocketSubprotocol();

            app.UseStaticFiles()
            .UseWebSockets()
            .MapWebSocketConnections("/socket", new WebSocketConnectionsOptions
            {
                SupportedSubProtocols = new List <ITextWebSocketSubprotocol>
                {
                    new JsonWebSocketSubprotocol(),
                    textWebSocketSubprotocol
                },
                DefaultSubProtocol = textWebSocketSubprotocol
            })
            .Run(async(context) =>
            {
                await context.Response.WriteAsync("-- Demo.AspNetCore.WebSocket --");
            });

            // Only for demo purposes, don't do this kind of thing to your production
            IWebSocketConnectionsService webSocketConnectionsService = serviceProvider.GetService <IWebSocketConnectionsService>();

            System.Threading.Thread webSocketHeartbeatThread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                while (true)
                {
                    webSocketConnectionsService.SendToAllAsync("Demo.AspNetCore.WebSockets Heartbeat", System.Threading.CancellationToken.None).Wait();
                    System.Threading.Thread.Sleep(5000);
                }
            }));
            webSocketHeartbeatThread.Start();
        }
Esempio n. 9
0
 internal async Task PushToSockets(List <FileIndexItem> updatedList)
 {
     var webSocketResponse =
         new ApiNotificationResponseModel <List <FileIndexItem> >(updatedList, ApiNotificationType.ManualBackgroundSync);
     await _connectionsService.SendToAllAsync(webSocketResponse, CancellationToken.None);
 }