use std::collections::BTreeMap; use std::sync::Arc; use std::sync::atomic::AtomicBool; use serde_json::json; use tokio::sync::Mutex; use tracing::trace; use exchanges::bingx_swap_rest::BingxSwapRest; use exchanges::bingx_swap_ws::{BingxSwapLogin, BingxSwapSubscribeType, BingxSwapWs, BingxSwapWsType}; use exchanges::response_base::ResponseData; const ACCESS_KEY: &str = ""; const SECRET_KEY: &str = ""; const PASS_KEY: &str = ""; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn ws_custom_subscribe() { global::log_utils::init_log_with_trace(); let (write_tx, write_rx) = futures_channel::mpsc::unbounded(); let (_, mut read_rx) = futures_channel::mpsc::unbounded::(); // let (write_tx, write_rx) = tokio::sync::broadcast::channel::(10); // let (read_tx, mut read_rx) = tokio::sync::broadcast::channel::(10); let write_tx_am = Arc::new(Mutex::new(write_tx)); let is_shutdown_arc = Arc::new(AtomicBool::new(true)); //读取 let _is_shutdown_arc_clone = Arc::clone(&is_shutdown_arc); let _tr = tokio::spawn(async move { trace!("线程-数据读取-开启"); loop { // 从通道中接收并丢弃所有的消息,直到通道为空 while let Ok(Some(_)) = read_rx.try_next() { // 从通道中接收并丢弃所有的消息,直到通道为空 while let Ok(Some(_)) = read_rx.try_next() { // 消息被忽略 } } } // trace!("线程-数据读取-结束"); }); //写数据 // let bool_v2_clone = Arc::clone(&is_shutdown_arc); // let write_tx_clone = Arc::clone(&write_tx_am); // let su = ws.get_subscription(); // let tw = tokio::spawn(async move { // trace!("线程-数据写入-开始"); // loop { // tokio::time::sleep(Duration::from_millis(20 * 1000)).await; // // let close_frame = CloseFrame { // // code: CloseCode::Normal, // // reason: Cow::Borrowed("Bye bye"), // // }; // // let message = Message::Close(Some(close_frame)); // // // let message = Message::Text(su.clone()); // AbstractWsMode::send_subscribe(write_tx_clone.clone(), message.clone()).await; // trace!("发送指令成功"); // } // trace!("线程-数据写入-结束"); // }); let fun = move |data: ResponseData| { async move { // trace!("---传入的方法~~~~{:?}", data); } }; let param = BingxSwapLogin { access_key: ACCESS_KEY.to_string(), secret_key: SECRET_KEY.to_string(), pass_key: PASS_KEY.to_string(), }; let t1 = tokio::spawn(async move { let mut ws = get_ws(Option::from(param), BingxSwapWsType::PublicAndPrivate); ws.set_symbols(vec!["BTC_USDT".to_string(), "ETC_USDT".to_string()]); ws.set_subscribe(vec![ // BingxSwapSubscribeType::PuFuturesTrades, // BingxSwapSubscribeType::PuFuturesDepth, BingxSwapSubscribeType::PuFuturesRecords, ]); //链接 let bool_v3_clone = Arc::clone(&is_shutdown_arc); ws.ws_connect_async(bool_v3_clone, fun, &write_tx_am, write_rx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)"); trace!("test 唯一线程结束--"); }); tokio::try_join!(t1).unwrap(); trace!("当此结束"); trace!("重启!"); trace!("参考交易所关闭"); return; } fn get_ws(btree_map: Option, ws_type: BingxSwapWsType) -> BingxSwapWs { let bingx_ws = BingxSwapWs::new(false,btree_map, ws_type); bingx_ws } //查詢合約基礎信息 #[tokio::test] async fn rest_get_market_test() { global::log_utils::init_log_with_trace(); let mut ret = get_rest(); let req_data = ret.get_market(json!({ })).await; println!("bingx--查詢合約基礎信息--{:?}", req_data); } fn get_rest() -> BingxSwapRest { let mut btree_map: BTreeMap = BTreeMap::new(); btree_map.insert("access_key".to_string(), ACCESS_KEY.to_string()); btree_map.insert("secret_key".to_string(), SECRET_KEY.to_string()); btree_map.insert("pass_key".to_string(), PASS_KEY.to_string()); let bingx_exc = BingxSwapRest::new(false, btree_map.clone()); bingx_exc }