Marfusios / bitmex-client-websocket

🛠️ C# client for Bitmex websocket API
Apache License 2.0
64 stars 44 forks source link

Can't get websocket to completely dispose #40

Open HollandRisley opened 3 years ago

HollandRisley commented 3 years ago

Hi, I really need help closing all websocket connections. I can't seem to get it to stop.. it keeps calling the 'Reconnection happened'.. I have tried Disposing both the communicator and the client, and I set the whole instance of this class to null from the the parent. But then every 60 secs.. I get the message reconnection happened in account socket.. even though it didn't actually connect.. the reconnector lives on! Any ideas? Here's my code.. Thanks so much in advance for any pointers! :)

private async Task StartSubscriptionsAsync(BitmexOrderTopper bitmexOrderTopper, BitmexOrderManager bitmexOrderManager, BitmexPositionManager bitmexPositionManager, string API_KEY, string API_SECRET)
        {
            IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json", true, true).Build();
            var appSettings = config.GetSection("ExchangeURLs");
            Uri url = new Uri(appSettings["Socket"]);

            using (var communicator = new BitmexWebsocketCommunicator(url))
            {
                communicator.ReconnectTimeout = TimeSpan.FromMinutes(1);  

                communicator.ReconnectionHappened.Subscribe(type =>Log("Reconnection Happened"));

                using (var client = new BitmexWebsocketClient(communicator))
                {
                    SendSubscriptionRequests(client, API_KEY, API_SECRET).Wait();

                    SubscribeToStreams(client, bitmexOrderTopper, bitmexOrderManager, bitmexPositionManager);

                    await communicator.Start();

                    Task.Run(() => StartSendingPing(client, this, communicator));

                    exitEvent.WaitOne();
                    client.Dispose();
                    communicator.Dispose();
                }
            }

        }

        private static async Task StartSendingPing(BitmexWebsocketClient client, BitmexSocketManager theSocket, BitmexWebsocketCommunicator communicator)
        {
            while (theSocket.cancelToken)
            {
                await Task.Delay(1000 * 10);
                client.Send(new PingRequest());
            }
        }

        internal void CancelSocket()
        {
            exitEvent.Close();
            cancelToken = false;
        }
HollandRisley commented 3 years ago

@Marfusios - I simplified the code in the question above.. Any idea how I can completely kill all threads including the reconnector? It just keeps trying to reconnect even though I've disposed the communicator and client.