コード例 #1
0
        public async Task WriteNotifications(Func <string, bool> shouldWriteByDb)
        {
            var receiveBuffer = new ArraySegment <byte>(new byte[1024]);
            var receive       = _webSocket.ReceiveAsync(receiveBuffer, _resourceShutdown);

            var asyncQueue = new AsyncQueue <DynamicJsonValue>();

            try
            {
                var sp = shouldWriteByDb == null ? null : Stopwatch.StartNew();
                using (_notificationsBase.TrackActions(asyncQueue, this))
                {
                    while (_resourceShutdown.IsCancellationRequested == false)
                    {
                        // we use this to detect client-initialized closure
                        if (receive.IsCompleted)
                        {
                            break;
                        }

                        var tuple = await asyncQueue.TryDequeueAsync(TimeSpan.FromSeconds(5));

                        if (tuple.Item1 == false)
                        {
                            await SendHeartbeat();

                            continue;
                        }

                        if (shouldWriteByDb != null &&
                            shouldWriteByDb((string)tuple.Item2["Database"]) == false)
                        {
                            if (sp.ElapsedMilliseconds > 5000)
                            {
                                sp.Restart();
                                await SendHeartbeat();
                            }

                            continue;
                        }

                        await WriteToWebSocket(tuple.Item2);
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
        }
コード例 #2
0
        public async Task WriteNotifications(Func <string, bool> shouldWriteByDb, Action firstTime = null)
        {
            var receiveBuffer = new ArraySegment <byte>(new byte[1024]);
            var receive       = _webSocket.ReceiveAsync(receiveBuffer, _resourceShutdown);

            var asyncQueue = new AsyncQueue <DynamicJsonValue>();

            try
            {
                using (_notificationsBase.TrackActions(asyncQueue, this))
                {
                    firstTime?.Invoke();
                    while (_resourceShutdown.IsCancellationRequested == false)
                    {
                        // we use this to detect client-initialized closure
                        if (receive.IsCompleted)
                        {
                            break;
                        }

                        var tuple = await asyncQueue.TryDequeueAsync(TimeSpan.FromSeconds(5));

                        if (tuple.Item1 == false)
                        {
                            await _webSocket.SendAsync(WebSocketHelper.Heartbeat, WebSocketMessageType.Text, true, _resourceShutdown);

                            continue;
                        }

                        if (shouldWriteByDb != null &&
                            shouldWriteByDb((string)tuple.Item2["Database"]) == false)
                        {
                            continue;
                        }

                        await WriteToWebSocket(tuple.Item2);
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
        }
コード例 #3
0
        public async Task WriteNotifications(Func <string, bool> shouldWriteByDb)
        {
            var receiveBuffer = new ArraySegment <byte>(new byte[1024]);
            var receive       = _webSocket.ReceiveAsync(receiveBuffer, _resourceShutdown);

            var asyncQueue = new AsyncQueue <DynamicJsonValue>();

            try
            {
                using (_notificationsBase.TrackActions(asyncQueue, this, shouldWriteByDb))
                {
                    AfterTrackActionsRegistration?.Invoke();

                    while (_resourceShutdown.IsCancellationRequested == false)
                    {
                        // we use this to detect client-initialized closure
                        if (receive.IsCompleted)
                        {
                            break;
                        }

                        var tuple = await asyncQueue.TryDequeueAsync(TimeSpan.FromSeconds(5));

                        if (tuple.Item1 == false)
                        {
                            await SendHeartbeat();

                            continue;
                        }

                        await WriteToWebSocket(tuple.Item2);
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
        }