use tungstenite::{connect, Message};
use url::Url;
static BINANCE_WS_API: &str = "wss://stream.binance.com:9443";
fn main() {
// 拼接完整的url,例如wss://stream.binance.com:9443/ws/btcusdt@trade
let url = format!("{}/ws/btcusdt@trade", BINANCE_WS_API);
// 使用connect函数来建立一个WebSocket连接,并且使用unwrap()方法处理错误
let (mut socket, response) = connect(Url::parse(&url).unwrap()).unwrap();
println!("Connected to binance stream.");
println!("HTTP status code: {}", response.status());
println!("Response headers:");
for (ref header, header_value) in response.headers() {
println!("* {}: {:?}", header, header_value);
}
loop {
// 使用read_message()方法来读取WebSocket消息,并且使用unwrap()方法处理错误
let msg = socket.read_message().unwrap();
// 如果消息是文本类型,就打印出来
if msg.is_text() {
println!("{}", msg);
}
}
}
所以是因为endpoints 拒绝了吗 400的code 这个怎么处理呀
tungstenite
does not enable support for TLS by default. You need to choose an appropriate feature in Cargo.toml.
For example,
[dependencies]
tungstenite = { version = "0.19", features = ["native-tls"] }
to use your OS’s native TLS implementation.
The HTTP error body says that you’re trying to send unencrypted HTTP request to an HTTPS endpoint.
1 Like