binance / binance-connector-dotnet

Lightweight connector for integration with Binance API
MIT License
207 stars 69 forks source link

Not receiving any websocket packets #31

Closed harrystuart closed 1 year ago

harrystuart commented 1 year ago
public class BinanceService
    {
        private BinanceServiceConfiguration mConfiguration { get; set; }
        private HttpClient mHttpClient { get; set; }
        private MarketDataWebSocket mMarketDataWebSocket { get; set; }
        private Mock<IBinanceWebSocketHandler> mSocketHandler { get; set; }
        private BinanceOrderBook mBinanceOrderBook { get; set; }

        public BinanceService()
        {
            mHttpClient = new HttpClient();

            IConfiguration config = new ConfigurationBuilder()
                .AddJsonFile("configuration.json")
                .Build();

            mConfiguration = config.GetRequiredSection("binanceService").Get<BinanceServiceConfiguration>();

            mSocketHandler = new Mock<IBinanceWebSocketHandler>();
        }

        public async Task InitialiseAsync(string symbol)
        {
            mMarketDataWebSocket = new MarketDataWebSocket($"{symbol}@depth", mSocketHandler.Object);

            mMarketDataWebSocket.OnMessageReceived((data) => {
                OnMessageReceived(data);
                return Task.CompletedTask;
            }, CancellationToken.None);

            Market market = new Market(mHttpClient);
            mBinanceOrderBook = JsonConvert.DeserializeObject<BinanceOrderBook>(await market.OrderBook(symbol, 5000));

            await mMarketDataWebSocket.ConnectAsync(CancellationToken.None);
            mSocketHandler.Verify(mock => mock.ConnectAsync(It.Is<Uri>(uri => uri.AbsolutePath == $"/ws/{symbol}@depth"), It.IsAny<CancellationToken>()), Times.Once());
        }

        public async Task RunBotContinuouslyAsync()
        {
            // DO STUFF HERE
        }

        private void OnMessageReceived(string data)
        {
            Console.WriteLine("test");
            BinanceOrderBook orderbookUpdates = JsonConvert.DeserializeObject<BinanceOrderBook>(data);

            foreach ((double price, double volume) in orderbookUpdates.Asks)
            {
                if (mBinanceOrderBook.Asks.ContainsKey(price))
                {
                    mBinanceOrderBook.Asks[price] = volume;
                }
                else
                {
                    mBinanceOrderBook.Asks.Add(price, volume);
                }
            }

            foreach ((double price, double volume) in orderbookUpdates.Bids)
            {
                if (mBinanceOrderBook.Bids.ContainsKey(price))
                {
                    mBinanceOrderBook.Bids[price] = volume;
                }
                else
                {
                    mBinanceOrderBook.Bids.Add(price, volume);
                }
            }
        }
    }

var binanceService = new BinanceService();
await binanceService.InitialiseAsync("BTCUSDT");
Task continuousBotTask = binanceService.RunBotContinuouslyAsync();
Task.Run(() => continuousBotTask);

Thread.Sleep(Timeout.Infinite);

I don't seem to receive any packets from the WS using the above implementation. The verification does not throw so presumably the connection is live. Any thoughts?

2pd commented 1 year ago

please use lower case symbol. btcusdt

harrystuart commented 1 year ago

This throws the exception Binance.Common.BinanceClientException: 'Illegal characters found in parameter 'symbol'; legal range is '^[A-Z0-9-_.]{1,20}$'.'.


Okay, fixed the above issue. Seems like the http client requires an uppercase symbol, and the ws client requires a lowercase symbol which is odd. Can mark as closed.