/// <summary>
        /// Disconnect the WebSocket and dispose the objects involved
        /// </summary>
        public void DisconnectAndDispose()
        {
            logger.LogInformation("DisconnectAndDispose() is being called");
            PureWebSocket pureWebSocket = new PureWebSocket(webSocketConfiguration.FullUrl, socketOptions);

            pureWebSocket?.Disconnect();
            pureWebSocket?.Dispose();
        }
Example #2
0
        public void Close()
        {
            _webSocket.OnStateChanged -= _webSocket_OnStateChanged;
            _webSocket.OnMessage      -= _webSocket_OnMessage;
            _webSocket.OnClosed       -= _webSocket_OnClosed;
            _webSocket.OnSendFailed   -= _webSocket_OnSendFailed;
            _webSocket.OnData         -= _webSocket_OnData;

            _webSocket.Disconnect();
        }
Example #3
0
        /// <summary>
        ///     Shutdown / Close the socket connection
        /// </summary>
        public void Disconnect()
        {
            _disconnectCalled = true;
            SignalR.Abort(_httpHost, _connectionToken, _hubs);
            _webSocket?.Disconnect();

            if (Debug)
            {
                WriteLine("Disconnect Called");
            }
        }
Example #4
0
        public async Task Stop()
        {
            _logger.Debug("Stopping Hubitat Client");
            if (_webSocket.State != WebSocketState.Closed)
            {
                _logger.Debug("Closing WebSocket");
                _webSocket.Disconnect();
            }

            Metrics.Close();
        }
Example #5
0
        /// <summary>
        ///     Shutdown / Close the socket connection
        /// </summary>
        public void Disconnect()
        {
            _disconnectCalled = true;
            SignalR.Abort(_httpHost, _connectionToken, _hubs, _options.IgnoreCertErrors);
            _webSocket?.Disconnect();

            if (_options.DebugMode)
            {
                WriteLine("Disconnect Called");
            }
        }
Example #6
0
 protected override void DoClose()
 {
     if (Ws != null)
     {
         try
         {
             Ws.Disconnect();
         }
         catch (Exception e)
         {
             Logger.Log("DoClose ws.Close() Exception= " + e.Message);
         }
     }
 }
Example #7
0
        public void Disconnect(Action callback)
        {
            if (_conn != null)
            {
                // _conn.OnClose(); //TODO how to clear event handler?
                if (_conn.State == WebSocketState.Open)
                {
                    _conn.Disconnect();
                }

                _conn.Dispose();
                _conn = null;
            }

            callback?.Invoke();
        }
        protected override async Task GetExchangeDataAsync(CancellationToken ct)
        {
            var options = new PureWebSocketOptions
            {
                DebugMode = false
            };

            using (var ws = new PureWebSocket(TickerUrl, options))
            {
                var closed = false;

                ws.OnMessage += WsOnMessage;
                ws.OnClosed  += reason =>
                {
                    Logger.Info($"Binance closed: {reason}");
                    closed = true;
                };
                ws.OnError += exception =>
                {
                    Logger.Error(exception);
                    closed = true;
                };

                if (!ws.Connect( ))
                {
                    Logger.Error("Couldn't connect to Binance");
                }

                while (ws.State != WebSocketState.Closed)
                {
                    if (UpTime > LastUpdateDuration &&
                        LastUpdateDuration > TimeSpan.FromHours(1) ||
                        closed)
                    {
                        ws.Disconnect( );
                        break;
                    }

                    await Task.Delay(PollingRate, ct).ConfigureAwait(false);
                }
            }
        }
Example #9
0
        private void ParseError(ErrorDataMessage data)
        {
            // the server requires SSL
            if (data.code == 4000)
            {
                RaiseError(new Exception(data.code + "|" + data.message));
                Disconnect();
                _purePusherClient.Options.SetSslOptionOn();
                Connect();
            }

            // the server does not like something and wants us to go away
            if (data.code >= 4001 && data.code < 4100)
            {
                RaiseError(new Exception(data.code + "|" + data.message));
                _websocket.Disconnect();
                return;
            }

            // the server wants us to try again in a bit (possibly due to being over capacity)
            if (data.code >= 4100 && data.code < 4200)
            {
                Disconnect();
                RaiseError(new Exception(data.code + "|" + data.message));
                Task.Delay(1500).Wait(); // block for a while
                Connect();
                return;
            }

            // the server wants us to reconnect
            if (data.code >= 4200 && data.code < 4300)
            {
                Disconnect();
                RaiseError(new Exception(data.code + "|" + data.message));
                Task.Delay(500).Wait(); // slight block
                Connect();
                return;
            }

            RaiseError(new Exception(data.code + "|" + data.message));
        }
Example #10
0
 public void ListenEvents(bool listen)
 {
     if (listen)
     {
         if (_ws != null)
         {
             if (_ws.State == WebSocketState.Open)
             {
                 return;
             }
         }
         Events.FireOnConnect(InitWs().Result);
     }
     else
     {
         if (_ws.State == WebSocketState.Open)
         {
             _ws.Disconnect();
         }
         Events.FireOnDisconnect();
     }
 }
Example #11
0
 public void Close()
 {
     ws.Disconnect();
 }
Example #12
0
 public void Disconnect()
 {
     Log("Disconnect invoked.");
     _socket.Disconnect();
 }
Example #13
0
 internal void Disconnect() => _websocket?.Disconnect();