rusty_common/websocket/exchanges/
binance.rs

1//! Binance WebSocket configuration
2//!
3//! Binance-specific WebSocket settings and handlers.
4
5use crate::types::Exchange;
6use crate::websocket::{CompressionConfig, WebSocketConfig};
7use std::time::Duration;
8
9/// Get default Binance WebSocket configuration
10pub fn default_config(url: String) -> WebSocketConfig {
11    WebSocketConfig::builder(Exchange::Binance, url)
12        .connect_timeout(Duration::from_secs(10))
13        .timeout(Duration::from_secs(30))
14        .ping_interval(Duration::from_secs(180)) // Binance recommends 3 minutes
15        .pong_timeout(Duration::from_secs(10))
16        .max_frame_size(65536) // 64KB
17        .max_message_size(10 * 1024 * 1024) // 10MB
18        // Binance does NOT support compression
19        .compression(CompressionConfig::disabled())
20        .build()
21}
22
23/// Binance WebSocket URLs
24pub mod urls {
25    /// Spot market WebSocket URL
26    pub const SPOT_WS: &str = "wss://stream.binance.com:9443/ws";
27
28    /// Spot market combined streams URL
29    pub const SPOT_COMBINED: &str = "wss://stream.binance.com:9443/stream";
30
31    /// Futures market WebSocket URL
32    pub const FUTURES_WS: &str = "wss://fstream.binance.com/ws";
33
34    /// Futures market combined streams URL
35    pub const FUTURES_COMBINED: &str = "wss://fstream.binance.com/stream";
36
37    /// Testnet spot WebSocket URL
38    pub const TESTNET_SPOT_WS: &str = "wss://testnet.binance.vision/ws";
39
40    /// Testnet futures WebSocket URL
41    pub const TESTNET_FUTURES_WS: &str = "wss://stream.binancefuture.com/ws";
42}
43
44/// Create a subscription message for Binance
45pub fn create_subscription(id: u64, method: &str, params: Vec<String>) -> simd_json::OwnedValue {
46    simd_json::json!({
47        "id": id,
48        "method": method,
49        "params": params
50    })
51}