binance / binance-spot-connector-rust

Simple Rust connector to Binance API
MIT License
139 stars 43 forks source link

Unable to Establish Connection with `BinanceWebSocketClient::connect_async #25

Open arthasyou opened 3 weeks ago

arthasyou commented 3 weeks ago

example/tokio_tungstenite.rs

I am facing an issue with the binance_spot_connector_rust library where the WebSocket connection attempt times out without any response. When executing the following code:

let (mut conn, _) = BinanceWebSocketClient::connect_async(BINANCE_WSS_BASE_URL)
        .await
        .expect("Failed to connect");

the connection hangs and eventually results in a timeout error.

Steps to Reproduce:

  1. Use the binance_spot_connector_rust crate to create a WebSocket connection to Binance.
  2. Set the WebSocket base URL as wss://stream.binance.com:9443/ws/btcusdt@miniTicker.
  3. Attempt to establish the connection using connect_async.

Expected Behavior: The connection should be established successfully and proceed to receive messages from the WebSocket stream.

Actual Behavior: The connection hangs indefinitely without any response, and after a while, a timeout error occurs.

Environment:

Additional Information:

I would appreciate any guidance on why the connection might be timing out and suggestions for potential fixes. Thank you!

arthasyou commented 3 weeks ago

Supplementary Information: To provide further context for my issue with binance_spot_connector_rust, I wrote a simple Go program that successfully connects to the Binance WebSocket endpoint and receives data without any issues. Here is the Go code:

package main

import (
    "log"
    "github.com/gorilla/websocket"
)

func main() {
    // WebSocket服务器地址
    url := "wss://stream.binance.com:443/ws/btcusdt@miniTicker"

    // 连接WebSocket服务器
    c, _, err := websocket.DefaultDialer.Dial(url, nil)
    if err != nil {
        log.Fatal("连接失败:", err)
    }
    defer c.Close()

    done := make(chan struct{})

    // 处理接收消息
    defer close(done)
    for i := 0; i < 10; i++ {
        _, message, err := c.ReadMessage()
        if err != nil {
            log.Println("读取消息错误:", err)
            return
        }
        log.Printf("收到消息: %s", message)
    }
}

Observation:

Comparison with Rust:

Request: I would appreciate any guidance or insight into why the Go implementation succeeds while the Rust code using binance_spot_connector_rust fails to establish a connection and times out. Any recommendations for resolving this discrepancy would be greatly appreciated.

arthasyou commented 3 weeks ago

I am facing an issue with a simple Rust program using the tungstenite crate to connect to a WebSocket server. The connection attempt fails, and the expected output line confirming the connection does not print. Here is the code snippet:

use tungstenite::{connect, Message};
use url::Url;

fn main() {
    // WebSocket server address
    let url = "wss://stream.binance.com:443/ws/btcusdt@miniTicker";

    // Connect to the WebSocket server
    let (mut socket, response) = connect(Url::parse(url).unwrap()).expect("Connection failed");

    println!(
        "Connection successful, HTTP status code: {}",
        response.status()
    );

    // Receive and print messages
    for _ in 0..10 {
        let msg = socket.read().expect("Failed to read message");
        match msg {
            Message::Text(text) => {
                println!("Received message: {}", text);
            }
            Message::Ping(ping) => {
                println!("Received Ping: {:?}", ping);
                // Respond with Pong
                socket
                    .send(Message::Pong(ping))
                    .expect("Failed to send Pong");
            }
            Message::Close(frame) => {
                println!("Connection closed: {:?}", frame);
                break;
            }
            _ => (),
        }
    }

    // Close the connection
    socket.close(None).expect("Failed to close connection");
}

Observation:

Expected Behavior: The program should print the HTTP status code after establishing a successful connection and proceed to read and print messages from the WebSocket server.

Actual Behavior: The connection attempt does not produce any output related to a successful connection, suggesting that the connection is not being completed or is failing silently.

Environment:

Relevant Cargo.toml Configuration:

[dependencies]
tungstenite = { version = "0.24", features = ["native-tls", "url"] }
url = "2.5"

Additional Information:

Request: I am looking for assistance in diagnosing why the connection using tungstenite does not succeed and does not print the expected line indicating connection success. Any insights into potential issues or required configurations would be greatly appreciated.