1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::WSClient;
use std::collections::HashMap;
use super::ws_client_internal::WSClientInternal;
use serde_json::{json, Value};
const SPOT_WEBSOCKET_URL: &str = "wss://stream.binance.com:9443/stream";
const FUTURES_WEBSOCKET_URL: &str = "wss://fstream.binance.com/stream";
const DELIVERY_WEBSOCKET_URL: &str = "wss://dstream.binance.com/stream";
pub struct BinanceSpotWSClient {
client: WSClientInternal,
}
pub struct BinanceFuturesWSClient {
client: WSClientInternal,
}
pub struct BinanceDeliveryWSClient {
client: WSClientInternal,
}
fn serialize_command(channels: &[String], subscribe: bool) -> Vec<String> {
let mut object = HashMap::<&str, Value>::new();
object.insert(
"method",
serde_json::to_value(if subscribe {
"SUBSCRIBE"
} else {
"UNSUBSCRIBE"
})
.unwrap(),
);
object.insert("params", json!(channels));
object.insert("id", json!(9527));
vec![serde_json::to_string(&object).unwrap()]
}
define_client!(BinanceSpotWSClient, SPOT_WEBSOCKET_URL, serialize_command);
define_client!(
BinanceFuturesWSClient,
FUTURES_WEBSOCKET_URL,
serialize_command
);
define_client!(
BinanceDeliveryWSClient,
DELIVERY_WEBSOCKET_URL,
serialize_command
);