コード例 #1
0
        public async Task StartSendingNotifications()
        {
            try
            {
                while (_cancellationTokenSource.IsCancellationRequested == false)
                {
                    var result = await _manualResetEvent.WaitAsync(TimeSpan.FromMilliseconds(5000)).ConfigureAwait(false);

                    if (IsAlive == false)
                    {
                        return;
                    }

                    if (result == false)
                    {
                        await SendMessage(WebSocketHelper.Heartbeat).ConfigureAwait(false);

                        continue;
                    }

                    _manualResetEvent.Reset();

                    while (_messages.TryDequeue(out var message))
                    {
                        if (IsAlive == false)
                        {
                            return;
                        }

                        await SendMessage(ToByteArraySegment(message)).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info("Error when handling web socket connection", e);
                }
            }
            finally
            {
                TrafficWatchManager.Disconnect(this);

                try
                {
                    if (_disposed == false)
                    {
                        await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "NORMAL_CLOSE", _cancellationTokenSource.Token);
                    }
                }
                catch
                {
                    // ignore
                }
            }
        }
コード例 #2
0
        public async Task TrafficWatchWebsockets()
        {
            using (var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync())
            {
                JsonOperationContext context;
                using (ServerStore.ContextPool.AllocateOperationContext(out context))
                {
                    try
                    {
                        var resourceName = GetStringQueryString("resourceName", required: false);
                        var connection   = new TrafficWatchConnection(webSocket, ServerStore.ServerShutdown,
                                                                      "db/" + resourceName);
                        TrafficWatchManager.AddConnection(connection);
                        await connection.StartSendingNotifications();
                    }
                    catch (IOException)
                    {
                        // nothing to do - connection closed
                    }
                    catch (Exception ex)
                    {
                        if (Logger.IsInfoEnabled)
                        {
                            Logger.Info("Error encountered in TrafficWatch handler", ex);
                        }

                        try
                        {
                            using (var ms = new MemoryStream())
                            {
                                using (var writer = new BlittableJsonTextWriter(context, ms))
                                {
                                    context.Write(writer, new DynamicJsonValue
                                    {
                                        ["Exception"] = ex
                                    });
                                }

                                ArraySegment <byte> bytes;
                                ms.TryGetBuffer(out bytes);
                                await webSocket.SendAsync(bytes, WebSocketMessageType.Text, true, ServerStore.ServerShutdown);
                            }
                        }
                        catch (Exception)
                        {
                            if (Logger.IsInfoEnabled)
                            {
                                Logger.Info("Failed to send the error in TrafficWatch handler to the client", ex);
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
        public async Task StartSendingNotifications()
        {
            try
            {
                while (_cancellationTokenSource.IsCancellationRequested == false)
                {
                    var result = await _manualResetEvent.WaitAsync(TimeSpan.FromMilliseconds(5000)).ConfigureAwait(false);

                    if (_cancellationTokenSource.IsCancellationRequested)
                    {
                        break;
                    }

                    if (result == false)
                    {
                        await SendMessage(HeartbeatMessage).ConfigureAwait(false);

                        continue;
                    }

                    _manualResetEvent.Reset();

                    TrafficWatchNotification message;
                    while (_msgs.TryDequeue(out message))
                    {
                        if (_cancellationTokenSource.IsCancellationRequested)
                        {
                            break;
                        }

                        await SendMessage(ToByteArraySegment(message)).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Info("Error when handling web socket connection", e);
                _cancellationTokenSource.Cancel();
            }
            finally
            {
                TrafficWatchManager.Disconnect(this);
                try
                {
                    await _websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "NORNAL_CLOSE", _cancellationTokenSource?.Token ?? CancellationToken.None);
                }
                catch
                {
                    // ignore
                }
            }
        }