Giter Site home page Giter Site logo

Comments (5)

JKorf avatar JKorf commented on May 23, 2024

Currently no. You can detect the socket closing using the BinanceStreamSubscription object returned when subscribing, but you'll just have to manually resubscribe. Might be a good addition though.

from binance.net.

snrchang avatar snrchang commented on May 23, 2024

Ok thanks

from binance.net.

RonaldSwaine avatar RonaldSwaine commented on May 23, 2024

I've been using things like this in my console apps:

        public static void Subscribe(bool autoReconnect, params string[] symbols)
        {
            foreach (var symbol in symbols)
            {
                Console.Write(string.Concat("Subscribing to ", symbol, " ..."));

                var sub = _socket.SubscribeToKlineStream(symbol, Interval, HandleIncomingKline);
                sub.Data.Closed += () => { Sub_Closed(symbol, autoReconnect); };
                sub.Data.Error += (e) => { Sub_Error(symbol, e); };

                if(!sub.Success)
                {
                    Console.WriteLine(string.Concat("Failed: (", sub.Error.Code,") ", sub.Error.Message));
                    continue;
                }

                _subs[symbol] = sub.Data;

                Console.WriteLine("Connected");
            }
        }

        private static void Sub_Error(string symbol, Exception e)
        {
            Console.WriteLine(string.Concat("Sub Error w/ ", symbol,": ", e.Message));
        }

        private static void Sub_Closed(string symbol, bool autoReconnect = true)
        {
            Console.WriteLine(string.Concat("Sub Closed: ", symbol));
            if(autoReconnect)
            {
                Console.WriteLine(string.Concat("Reconnecting to ", symbol));

                // dont auto reconnect this sub since we can't impose a delay without dealing with threads
                Subscribe(false, symbol);
                // but if you wanted to make Sub_Closed async, you could also do:
                // Console.WriteLine(string.Concat("Reconnecting to ", symbol, " in 5s"));
                // await System.Threading.Tasks.Task.Delay(5000);
            }
        }

from binance.net.

JKorf avatar JKorf commented on May 23, 2024

I've implemented an auto reconnect try. When the connection gets closed it will try to reconnect. This will handle the auto disconnect Binance does every 24 hours automatically. For reconnecting after loss of internet a solution as @RonaldSwaine provided should do. I'll probably implement some method to be able to reconnect a subscription easier later on.

from binance.net.

JKorf avatar JKorf commented on May 23, 2024

I've implemented auto reconnecting in the latest version (v3.0.10)

from binance.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.