websocket 账户信息推送连接不了啊?

我全程开着代理,就是连接不了。

use anyhow::anyhow;
use lazy_static::lazy_static;
use reqwest::Client;
use serde_json::json;
use serde_json::value;
use serde_json::Value;
use std::time::Duration;
use websockets::WebSocket;

lazy_static! {
    static ref CLIENT: Client = Client::builder()
        .timeout(Duration::from_secs(5))
        .build()
        .unwrap();
    static ref API_KEY: &'static str =
        "*";
    static ref SECRET_KEY: &'static str =
        "*";
}

fn sha256(text: &str, key: &str) -> String {
    base64::Engine::encode(
        &base64::engine::general_purpose::STANDARD,
        ring::hmac::sign(
            &ring::hmac::Key::new(ring::hmac::HMAC_SHA256, key.as_bytes()),
            text.as_bytes(),
        ),
    )
}

async fn get_listen_key() -> anyhow::Result<String> {
    let result = CLIENT
        .post("https://fapi.binance.com/fapi/v1/listenKey")
        .header("X-MBX-APIKEY", *API_KEY)
        .send()
        .await?
        .json::<Value>()
        .await?;
    result["listenKey"]
        .as_str()
        .ok_or(anyhow!("{}", result))
        .map(|v| v.to_string())
}

async fn put_listen_key() -> anyhow::Result<()> {
    CLIENT
        .post(&format!("https://fapi.binance.com/fapi/v1/listenKey"))
        .header("X-MBX-APIKEY", *API_KEY)
        .send()
        .await?
        .error_for_status()?;
    Ok(())
}

async fn web_socket_connect(listen_key: String) -> anyhow::Result<WebSocket> {
    WebSocket::connect(&format!("wss://fstream.binance.com/ws/{listen_key}"))
        .await
        .map_err(|v| anyhow!(v))
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // 连接不了
    let mut ws = web_socket_connect(get_listen_key().await?).await?;
    Ok(())
}

是连接不成功么? 如果连接不上,试试换个代理?

1 Like