| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- use std::collections::BTreeMap;
- use std::sync::Arc;
- use std::sync::atomic::AtomicBool;
- use tokio::sync::mpsc::{channel, Sender};
- use tokio::try_join;
- use tracing::trace;
- use exchanges::binance_swap_rest::BinanceSwapRest;
- use exchanges::binance_swap_ws::{BinanceSubscribeType, BinanceSwapWs, BinanceWsType};
- use exchanges::gate_swap_ws::{GateSubscribeType, GateSwapWs, GateWsType};
- use exchanges::proxy;
- use exchanges::response_base::ResponseData;
- const ACCESS_KEY: &str = "";
- const SECRET_KEY: &str = "";
- //ws-订阅公共频道信息
- #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
- async fn ws_custom_subscribe() {
- // global::log_utils::init_log_with_trace();
- //
- // let mut bool_v1 = Arc::new(AtomicBool::new(true));
- // let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
- // btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string());
- // btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string());
- // let (tx, mut rx) = channel(1024);
- // let mut ws = get_ws(btree_map, tx);
- // ws.set_subscribe(vec![
- // GateSubscribeType::PuFuturesCandlesticks,
- // GateSubscribeType::PuFuturesTrades,
- // GateSubscribeType::PuFuturesOrderBook,
- //
- // GateSubscribeType::PrFuturesPositions("".to_string()),
- // ]);
- // let t1 = tokio::spawn(async move {
- // ws.custom_subscribe(bool_v1, vec!["BTC_USDT".to_string()]).await;
- // });
- // let t2 = tokio::spawn(async move {
- // loop {
- // if let Ok(received) = rx.try_recv() {
- // trace!( "age: {:?}", received);
- // }
- // }
- // });
- // try_join!(t1,t2).unwrap();
- let json_str = r#"{"name": "Alice", "age": 30}"#;
- // 解析 JSON 字符串并将其转换为一个 Rust 结构体
- let parsed_data: Result<serde_json::Value, serde_json::Error> = serde_json::from_str(json_str);
- match parsed_data {
- Ok(data) => {
- println!("Name: {}", data["name"]);
- println!("Age: {}", data["age"]);
- }
- Err(e) => {
- println!("Failed to parse JSON: {}", e);
- }
- }
- }
- fn get_ws(btree_map: BTreeMap<String, String>, tx: Sender<ResponseData>) -> GateSwapWs {
- let mut binance_ws = GateSwapWs::new(false,
- btree_map,
- GateWsType::PublicAndPrivate("usdt".to_string()),
- tx);
- binance_ws
- }
- fn get_rest() -> BinanceSwapRest {
- let mut btree_map: BTreeMap<String, String> = BTreeMap::new();
- btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string());
- btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string());
- let mut ba_exc = BinanceSwapRest::new(false, btree_map);
- ba_exc
- }
|