Beispiel #1
0
        /// <summary>
        /// Connect to the All Market Symbol Ticker WebSocket
        /// </summary>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToIndividualSymbolTickerWebSocket(BinanceWebSocketMessageHandler <BinanceAggregateTradeData> messageEventHandler)
        {
            Logger.Debug("Connecting to All Market Symbol Ticker Web Socket");
            var endpoint = new Uri($"{BaseWebsocketUri}/!ticker@arr");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }
        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);
        }
        /// <summary>
        /// Connect to the Depth WebSocket
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToDepthWebSocket(string symbol, BinanceWebSocketMessageHandler <BinanceDepthData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbol, nameof(symbol));
            Logger.Debug("Connecting to Depth Web Socket");
            var endpoint = new Uri($"{BaseWebsocketUri}/{symbol.ToLower()}@depth");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }
        /// <summary>
        /// Connect to the Kline WebSocket
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="interval"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToKlineWebSocket(string symbol, KlineInterval interval, BinanceWebSocketMessageHandler <BinanceKlineData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbol, nameof(symbol));
            Logger.Debug("Connecting to Kline Web Socket");
            var endpoint = new Uri($"{BaseWebsocketUri}/{symbol.ToLower()}@kline_{EnumExtensions.GetEnumMemberValue(interval)}");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }
        /// <summary>
        /// Connect to the Trades WebSocket
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>https://github.com/glitch100/BinanceDotNet/issues
        public async Task <Guid> ConnectToTradesWebSocketAsync(string symbol, BinanceWebSocketMessageHandler <BinanceAggregateTradeData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbol, nameof(symbol));
            Logger.Debug("Connecting to Trades Web Socket");
            var endpoint = new Uri($"{BaseWebsocketUri}/{symbol.ToLower()}@aggTrade");

            return(await CreateBinanceWebSocketAsync(endpoint, messageEventHandler));
        }
        /// <summary>
        /// Connect to the Individual Symbol Ticker WebSocket
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToIndividualSymbolTickerWebSocket(string symbol, BinanceWebSocketMessageHandler <BinanceAggregateTradeData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbol, nameof(symbol));
            Logger.Debug("Connecting to Individual Symbol Ticker Web Socket");
            var endpoint = new Uri($"{BaseWebsocketUri}/{symbol.ToLower()}@ticker");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }
        /// <summary>
        /// Connect to the Combined Depth WebSocket
        /// </summary>
        /// <param name="symbols"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToDepthWebSocketCombined(string symbols, BinanceWebSocketMessageHandler <BinanceCombinedDepthData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbols, nameof(symbols));
            symbols = PrepareCombinedSymbols.CombinedDepth(symbols);
            Logger.Debug("Connecting to Combined Depth Web Socket");
            var endpoint = new Uri($"{CombinedWebsocketUri}={symbols}");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }
Beispiel #8
0
        /// <summary>
        /// Connect to the Kline WebSocket
        /// </summary>
        /// <param name="symbols"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToTradesWebSocketCombined(string[] symbols, BinanceWebSocketMessageHandler <CombinedBinanceAggregateTradeData> messageEventHandler)
        {
            var symbolName = new StringBuilder();

            for (int i = 0; i < symbols.Length; i++)
            {
                string symbol = symbols[i];
                if (i > 0)
                {
                    symbolName.Append('/');
                }
                symbolName.Append(symbol).Append("@aggTrade");
            }

            var endpoint = new Uri($"{CombinedWebsocketUri}={symbolName.ToString()}");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }
Beispiel #9
0
        /// <summary>
        /// Connect to the Combined Depth WebSocket
        /// </summary>
        /// <param name="symbols"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToDepthWebSocketCombined(List <string> symbols, BinanceWebSocketMessageHandler <BinanceCombinedDepthData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbols, nameof(symbols));
            var combinedsymbols = new StringBuilder();

            for (int i = 0; i < symbols.Count; i++)
            {
                string symbol = symbols[i];
                if (i > 0)
                {
                    combinedsymbols.Append('/');
                }
                combinedsymbols.Append(symbol.ToLower()).Append("@depth");
            }
            Logger.Debug("Connecting to Combined Depth Web Socket");
            var endpoint = new Uri($"{CombinedWebsocketUri}={combinedsymbols}");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }
        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>
        /// Connect to thePartial Book Depth Streams
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public async Task <Guid> ConnectToPartialDepthWebSocketAsync(string symbol, PartialDepthLevels levels, BinanceWebSocketMessageHandler <BinancePartialData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbol, nameof(symbol));
            Logger.Debug("Connecting to Partial Depth Web Socket");
            var endpoint = new Uri($"{BaseWebsocketUri}/{symbol.ToLower()}@depth{(int)levels}");

            return(await CreateBinanceWebSocketAsync(endpoint, messageEventHandler));
        }
        /// <summary>
        /// Connect to the Combined Partial Depth WebSocket
        /// </summary>
        /// <param name="symbols"></param>
        /// <param name="depth"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public async Task <Guid> ConnectToPartialDepthWebSocketCombinedAsync(string symbols, string depth, BinanceWebSocketMessageHandler <BinancePartialDepthData> messageEventHandler)
        {
            Guard.AgainstNullOrEmpty(symbols, nameof(symbols));
            Guard.AgainstNullOrEmpty(depth, nameof(depth));
            symbols = PrepareCombinedSymbols.CombinedPartialDepth(symbols, depth);
            Logger.Debug("Connecting to Combined Partial Depth Web Socket");
            var endpoint = new Uri($"{CombinedWebsocketUri}={symbols}");

            return(await CreateBinanceWebSocketAsync(endpoint, messageEventHandler));
        }
Beispiel #13
0
        /// <summary>
        /// Connect to the Kline WebSocket
        /// </summary>
        /// <param name="symbols"></param>
        /// <param name="messageEventHandler"></param>
        /// <returns></returns>
        public Guid ConnectToKlineWebSocketCombined(string[] symbols, KlineInterval interval, BinanceWebSocketMessageHandler <CombinedBinanceKlineData> messageEventHandler)
        {
            var symbolName = new StringBuilder();

            for (int i = 0; i < symbols.Length; i++)
            {
                string symbol = symbols[i];
                if (i > 0)
                {
                    symbolName.Append('/');
                }
                symbolName.Append(symbol.ToLower()).Append("@kline_" + EnumExtensions.GetEnumMemberValue(interval));
            }

            var endpoint = new Uri($"{CombinedWebsocketUri}={symbolName.ToString()}");

            return(CreateBinanceWebSocket(endpoint, messageEventHandler));
        }