Coder Social home page Coder Social logo

Comments (3)

wisespace-io avatar wisespace-io commented on August 23, 2024

@fredfortier Unfortunately, there is not. Feel free to submit a PR.

Ps.: I will be unresponsive for few days, xmas holiday.

from binance-rs.

wisespace-io avatar wisespace-io commented on August 23, 2024

@fredfortier I added a way to break the event_loop, not sure if it is the best solution. Basically, it allows the user breaks the event loop on application level by using a Boolean.

In the example below, it watches the Bitcoin price and if it reaches 7300, it breaks the event loop and disconnects from the websocket.

fn last_price() {
    let keep_running = AtomicBool::new(true); // It is used to control the event_loop
    let agg_trade: String = format!("!ticker@arr");
    let mut btcusdt: f32 = "0".parse().unwrap();

    let mut web_socket: WebSockets = WebSockets::new(|event: WebsocketEvent| {
        match event {
            WebsocketEvent::DayTicker(ticker_events) => {
                for tick_event in ticker_events {
                    if tick_event.symbol == "BTCUSDT" {
                        btcusdt = tick_event.average_price.parse().unwrap();
                        let btcusdt_close: f32 = tick_event.current_close.parse().unwrap();
                        println!("{} - {}", btcusdt, btcusdt_close);

                        if btcusdt_close as i32 == 7300 {
                            // Break the event loop
                            keep_running.store(false, Ordering::Relaxed);
                        }
                    }
                }
            },
            _ => (),
        };

        Ok(())
    });

    web_socket.connect(&agg_trade).unwrap(); // check error
    if let Err(e) = web_socket.event_loop(&keep_running) {
        match e {
            err => {
               println!("Error: {}", err);
            }
        }
    }
    web_socket.disconnect().unwrap();
    println!("disconnected");
}

So, I changed the event_loop signature and introduced a disconnect method.

pub fn event_loop(&mut self, running: &AtomicBool) -> Result<()> {
    ...
}

Please let me know if it works for your case and if you have a suggestion or better solution.

from binance-rs.

wisespace-io avatar wisespace-io commented on August 23, 2024

@fredfortier I will close this issue, please reopen it if it did not work as expected. I will also add async support in a few days to the library.

from binance-rs.

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.