Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
arthasyou opened this issue Nov 2, 2024 · 2 comments

Comments

@arthasyou
Copy link

arthasyou commented Nov 2, 2024

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:

  • Operating System: [Insert OS version]
  • Rust version: [Insert version]
  • binance_spot_connector_rust version: [Insert version]

Additional Information:

  • The WebSocket URL has been verified as correct.
  • Network connectivity is confirmed.
  • No error logs or responses are provided by the connection attempt until the timeout.

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

@arthasyou
Copy link
Author

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:

  • The Go program connects to wss://stream.binance.com:443/ws/btcusdt@miniTicker and receives data as expected.
  • This indicates that the WebSocket server is functioning correctly and the URL is valid.

Comparison with Rust:

  • In contrast, when using binance_spot_connector_rust to establish the same connection in Rust, the connection attempt hangs and eventually times out, suggesting a potential issue with the Rust implementation or library.

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
Copy link
Author

arthasyou commented Nov 2, 2024

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:

  • The line println!("Connection successful, HTTP status code: {}", response.status()); does not print, which suggests that the connection is not established.
  • The expect("Connection failed") does not panic, indicating the issue might occur after the initial connection attempt or during the handshake.

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:

  • OS: [Insert OS version]
  • Rust version: [Insert version]
  • tungstenite version: 0.24
  • url version: 2.5

Relevant Cargo.toml Configuration:

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

Additional Information:

  • The WebSocket server URL is known to be valid and works with other programming languages, such as Go, as confirmed with similar tests.
  • Network connectivity has been verified, and there are no known firewall or proxy issues affecting the connection.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant