Exemple #1
0
 private void Init()
 {
     _transport.MessageReceived     += ReceiveMessage;
     _transport.DataConnected       += () => DataConnected?.Invoke();
     _transport.DataReconnected     += () => DataReconnected?.Invoke();
     _transport.DataDisconnected    += () => DataDisconnected?.Invoke();
     _transport.ControlConnected    += () => ControlConnected?.Invoke();
     _transport.ControlReconnected  += () => ControlReconnected?.Invoke();
     _transport.ControlDisconnected += () => ControlDisconnected?.Invoke();
 }
        private async Task OpenTradeEvents()
        {
            string prefix = SecureConnection ? "wss" : "ws";
            var    uri    = new Uri($"{prefix}://{HostName}/events/order/");

            OrderSocket = new WebsocketClient(uri, GetWSFactory(true));
            OrderSocket.ReconnectTimeout      = null;
            OrderSocket.IsReconnectionEnabled = true;
            OrderSocket.ReconnectionHappened.Subscribe(info =>
            {
                logger.Warn($"Reconnection happened, reason: {info.Type}, stream: trade");
                System.Diagnostics.Debug.WriteLine($"Reconnection happened, type: {info.Type}, stream: trade");
                if (info.Type != ReconnectionType.Initial)
                {
                    ControlReconnected?.Invoke();
                }
            });
            OrderSocket.DisconnectionHappened.Subscribe(info =>
            {
                logger.Error($"Disconnected, reason: {info.Type}, exception: {info.Exception}");
                System.Diagnostics.Debug.WriteLine($"Disconnected, type: {info.Type}");
                ControlDisconnected?.Invoke();
                if (info.Type == DisconnectionType.ByServer || info.Type == DisconnectionType.NoMessageReceived)
                {
                    Task.Run(async() => await OpenTradeEvents());
                }
                else
                {
                    logger.Fatal($"Disconnected, reason: {info.Type}, exception: {info.Exception}");
                }
            });
            OrderSocket.MessageReceived.Subscribe(msg => MessageReceived?.Invoke(msg.Binary));
            await OrderSocket.Start();

            ControlConnected?.Invoke();
        }