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); }
/// <summary> /// Connect to the UserData WebSocket /// </summary> /// <param name="userDataMessageHandlers"></param> /// <returns></returns> public async Task <Guid> ConnectToUserDataWebSocket(UserDataWebSocketMessages userDataMessageHandlers) { Guard.AgainstNull(BinanceClient, nameof(BinanceClient)); Logger.Debug("Connecting to User Data Web Socket"); var streamResponse = await BinanceClient.StartUserDataStream(); var endpoint = new Uri($"{BaseWebsocketUri}/{streamResponse.ListenKey}"); return(CreateUserDataBinanceWebSocket(endpoint, userDataMessageHandlers)); }
private async Task <Guid> CreateUserDataBinanceWebSocketAsync(Uri endpoint, string listenKey, UserDataWebSocketMessages userDataWebSocketMessages) { var websocket = new BinanceWebSocket(listenKey); void onMsg(WebSocketWrapper sender, string msg) { Logger.Trace($"WebSocket Message Received on Endpoint: {endpoint.AbsoluteUri}"); var primitive = JsonConvert.DeserializeObject <BinanceWebSocketResponse>(msg); switch (primitive.EventType) { case AccountEventType: var userData = JsonConvert.DeserializeObject <BinanceAccountUpdateData>(msg); userDataWebSocketMessages.AccountUpdateMessageHandler(userData); break; case OrderTradeEventType: var orderTradeData = JsonConvert.DeserializeObject <BinanceTradeOrderData>(msg); if (orderTradeData.ExecutionType == ExecutionType.Trade) { userDataWebSocketMessages.TradeUpdateMessageHandler(orderTradeData); } else { userDataWebSocketMessages.OrderUpdateMessageHandler(orderTradeData); } break; case OutboundAccountPosition: var accountPosition = JsonConvert.DeserializeObject <OutboundAccountPosition>(msg); userDataWebSocketMessages?.OutboundAccountPositionHandler(accountPosition); break; case ListStatus: //todo break; default: Logger.Error("Unknown EventType for user data stream"); break; } } if (!ActiveWebSockets.ContainsKey(websocket.Id)) { ActiveWebSockets.Add(websocket.Id, websocket); } await websocket.ConnectAsync(endpoint, onMsg); return(websocket.Id); }