binance-rs icon indicating copy to clipboard operation
binance-rs copied to clipboard

websocket receive MarketData error

Open Ultraman95 opened this issue 2 years ago • 10 comments

thread 'main' panicked at 'called Result::unwrap() on an Err value: Error(Msg("Error during handshake URL error: Unable to connect to wss://stream.binance.com:9443/stream?streams=ethbtc@depth@100ms/bnbeth@depth@100ms"), State { next_error: None, backtrace: InternalBacktrace })'

Rust Version 1.53

Ultraman95 avatar Jul 14 '21 08:07 Ultraman95

I used vpn

Ultraman95 avatar Jul 14 '21 08:07 Ultraman95

@Ultraman95 It sounds like it could be solved by adding Proxy support to this crate.

wisespace-io avatar Jul 18 '21 09:07 wisespace-io

@wisespace-io I had this issue too, until I got rid of the port number from the URL in my script. Need a quick fix?

siegfried avatar Nov 12 '21 09:11 siegfried

@siegfried It is weird. The port is part of the url as stated in the Binance docs.

Have you tried to overwrite it? See https://github.com/wisespace-io/binance-rs#testnet-and-api-clusters. All URLs can be overwritten with a custom config.

let config = Config::default().set_ws_endpoint("wss://stream.binance.com/ws");
let user_stream: UserStream = Binance::new_with_config(api_key_user, None, &config);

See where it happens, https://github.com/wisespace-io/binance-rs/blob/master/src/websockets.rs#L23

wisespace-io avatar Nov 12 '21 11:11 wisespace-io

Thanks. I haven't tried this crate. Great work. Will do soon. This just reminded me the issue I had in my script a few weeks ago. I just checked the document, it seems they added the port number back.

siegfried avatar Nov 12 '21 11:11 siegfried

@wisespace-io The issue still exists. I got this error by running the example.

use binance::websockets::*;
use std::sync::atomic::{AtomicBool};

fn main() {
    let keep_running = AtomicBool::new(true); // Used to control the event loop
    let kline: String = format!("{}", "ethbtc@kline_1m");
    let mut web_socket: WebSockets = WebSockets::new(|event: WebsocketEvent| {
        match event {
            WebsocketEvent::Kline(kline_event) => {
                println!("Symbol: {}, high: {}, low: {}", kline_event.kline.symbol, kline_event.kline.low, kline_event.kline.high);
            },
            _ => (),
        };
        Ok(())
    });
 
    web_socket.connect(&kline).unwrap(); // check error
    if let Err(e) = web_socket.event_loop(&keep_running) {
        match e {
          err => {
             println!("Error: {:?}", err);
          }
        }
     }
     web_socket.disconnect().unwrap();
}
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error(Msg("Error during handshake TLS error: native-tls error: connection closed via error"), State { next_error: None, backtrace: InternalBacktrace })', src/main.rs:17:32

After remove the port number, it works.

use binance::websockets::{
    WebSockets,
    WebsocketEvent
};
use binance::config::Config;
use std::sync::atomic::{AtomicBool};

fn main() {
    let config = Config::default().set_ws_endpoint("wss://stream.binance.com/ws");
    let keep_running = AtomicBool::new(true); // Used to control the event loop
    let kline: String = format!("{}", "ethbtc@kline_1m");
    let mut web_socket: WebSockets = WebSockets::new(|event: WebsocketEvent| {
        match event {
            WebsocketEvent::Kline(kline_event) => {
                println!("Symbol: {}, high: {}, low: {}", kline_event.kline.symbol, kline_event.kline.low, kline_event.kline.high);
            },
            _ => (),
        };
        Ok(())
    });

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

I can help on changing the default and do some refactor if you need.

siegfried avatar Nov 15 '21 13:11 siegfried

@siegfried All binance libraries use the default port. As this crate gives the possibility of overwrite the url, wouldn't be enough?

 let config = Config::default().set_ws_endpoint("wss://stream.binance.com/ws");

Maybe we just need to add the example to the README in case someone else has problems with the default port. It seems more an issue with your connection. Check similar issue in https://github.com/ratchetphp/Pawl/issues/100

wisespace-io avatar Nov 17 '21 13:11 wisespace-io

I will add support to Proxy, it may also help.

wisespace-io avatar Nov 17 '21 13:11 wisespace-io

Add the example to the README is fine, I think. Thanks.

siegfried avatar Nov 17 '21 13:11 siegfried

@wisespace-io There is no config option for connect_multiple_streams. Maybe we should make the endpoint setting global?

siegfried avatar Dec 10 '21 15:12 siegfried