wisespace-io / binance-rs

Rust Library for the Binance API
Other
665 stars 297 forks source link

Breaking the event_loop #36

Closed fredfortier closed 4 years ago

fredfortier commented 4 years ago

I want to subscribe to a user stream up until a certain point (e.g. all open orders filled), then close the stream. I'm blocking a thread like this to watch the stream: ws.event_loop(). I don't see any method for this in the WebSockets struct. Is there a recommended way to break the loop?

If not, I can submit a PR. Any guidance on that would be appreciated.

wisespace-io commented 4 years ago

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

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

wisespace-io commented 4 years ago

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

wisespace-io commented 4 years ago

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