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); }
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); }
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); }
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); }
private async Task <Guid> CreateBinanceWebSocketAsync <T>(Uri endpoint, BinanceWebSocketMessageHandler <T> messageEventHandler) where T : IWebSocketResponse { var websocket = new BinanceWebSocket(); void onRecv(WebSocketWrapper sender, string msg) { Logger.Debug($"WebSocket Messge Received on: {endpoint.AbsoluteUri}"); //TODO: Log message received var data = JsonConvert.DeserializeObject <T>(msg); messageEventHandler(data); } await websocket.ConnectAsync(endpoint, onRecv); if (!ActiveWebSockets.ContainsKey(websocket.Id)) { ActiveWebSockets.Add(websocket.Id, websocket); } return(websocket.Id); }
/// <summary> /// Close a specific WebSocket instance using the Guid provided on creation /// If it is a user data stream socket then also the listenKey is closed /// </summary> public void CloseWebSocketInstance(Guid id) { BinanceWebSocket ws = null; lock (ActiveWebSockets) { if (ActiveWebSockets.ContainsKey(id)) { ws = ActiveWebSockets[id]; ActiveWebSockets.Remove(id); } else { throw new Exception($"No Websocket exists with the Id {id.ToString()}"); } } try { _ = ws.CloseAsync(); } catch { } if (ws.ListenKey != null) { _ = this.BinanceClient.CloseUserDataStream(ws.ListenKey); } }