private Guid CreateBinanceWebSocket <T>(Uri endpoint, BinanceWebSocketMessageHandler <T> messageEventHandler) where T : IWebSocketResponse
        {
            var websocket = new BinanceWebSocket(endpoint.AbsoluteUri);

            websocket.OnOpen += (sender, e) =>
            {
                Logger.Debug($"WebSocket Opened:{endpoint.AbsoluteUri}");
            };
            websocket.OnMessage += (sender, e) =>
            {
                Logger.Debug($"WebSocket Messge Received on: {endpoint.AbsoluteUri}");
                //TODO: Log message received
                var data = JsonConvert.DeserializeObject <T>(e.Data);
                messageEventHandler(data);
            };
            websocket.OnError += (sender, e) =>
            {
                Logger.Debug($"WebSocket Error on {endpoint.AbsoluteUri}:", e);
                CloseWebSocketInstance(websocket.Id, true);
                throw new Exception("Binance WebSocket failed")
                      {
                          Data =
                          {
                              { "ErrorEventArgs", e }
                          }
                      };
            };
            ActiveWebSockets.TryAdd(websocket.Id, websocket);
            AllSockets.Add(websocket);
            websocket.SslConfiguration.EnabledSslProtocols = SupportedProtocols;
            websocket.Connect();

            return(websocket.Id);
        }
예제 #2
0
        private Guid CreateUserDataBinanceWebSocket(Uri endpoint, UserDataWebSocketMessages userDataWebSocketMessages)
        {
            var websocket = new BinanceWebSocket(endpoint.AbsoluteUri);

            websocket.OnOpen += (sender, e) =>
            {
                Logger.Debug($"WebSocket Opened:{endpoint.AbsoluteUri}");
            };
            websocket.OnMessage += (sender, e) =>
            {
                Logger.Debug($"WebSocket Message Received on Endpoint: {endpoint.AbsoluteUri}");
                var primitive = JsonConvert.DeserializeObject <BinanceWebSocketResponse>(e.Data);
                switch (primitive.EventType)
                {
                case AccountEventType:
                    var userData = JsonConvert.DeserializeObject <BinanceAccountUpdateData>(e.Data);
                    userDataWebSocketMessages.AccountUpdateMessageHandler?.Invoke(userData);
                    break;

                case OrderTradeEventType:
                    var orderTradeData = JsonConvert.DeserializeObject <BinanceTradeOrderData>(e.Data);
                    if (orderTradeData.ExecutionType == ExecutionType.Trade)
                    {
                        userDataWebSocketMessages.TradeUpdateMessageHandler?.Invoke(orderTradeData);
                    }
                    else
                    {
                        userDataWebSocketMessages.OrderUpdateMessageHandler?.Invoke(orderTradeData);
                    }
                    break;

                default:
                    //throw new ArgumentOutOfRangeException();  //TODO:  CLEAN UP - Outbound position
                    break;
                }
            };
            websocket.OnError += (sender, e) =>
            {
                Logger.Error($"WebSocket Error on {endpoint.AbsoluteUri}: ", e.Exception);
                CloseWebSocketInstance(websocket.Id, true);
                throw new Exception("Binance UserData WebSocket failed")
                      {
                          Data =
                          {
                              { "ErrorEventArgs", e }
                          }
                      };
            };

            if (!ActiveWebSockets.ContainsKey(websocket.Id))
            {
                ActiveWebSockets.Add(websocket.Id, websocket);
            }

            AllSockets.Add(websocket);
            websocket.SslConfiguration.EnabledSslProtocols = SupportedProtocols;
            websocket.Connect();

            return(websocket.Id);
        }
예제 #3
0
        private Guid CreateBinanceWebSocket(Uri endpoint, OnMessageReceived messageEventHandler)
        {
            var websocket = new BinanceWebSocket(endpoint.AbsoluteUri);

            websocket.OnMessage += (sender, e) =>
            {
                messageEventHandler(e.Data);
            };

            if (!ActiveWebSockets.ContainsKey(websocket.Id))
            {
                ActiveWebSockets.Add(websocket.Id, websocket);
            }

            AllSockets.Add(websocket);
            websocket.SslConfiguration.EnabledSslProtocols = SupportedProtocols;
            websocket.Connect();

            return(websocket.Id);
        }