bitmex-client-websocket icon indicating copy to clipboard operation
bitmex-client-websocket copied to clipboard

Can't get websocket to completely dispose

Open HollandRisley opened this issue 4 years ago • 1 comments

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 avatar Mar 03 '21 18:03 HollandRisley

@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.

HollandRisley avatar Mar 04 '21 09:03 HollandRisley