| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- use std::sync::Arc;
- use std::sync::atomic::AtomicBool;
- use futures_util::StreamExt;
- use tokio::sync::Mutex;
- use tracing::trace;
- use exchanges::kucoin_swap_ws::{KucoinSwapLogin, KucoinSwapSubscribeType, KucoinSwapWs, KucoinSwapWsType};
- const ACCESS_KEY: &str = "";
- const SECRET_KEY: &str = "";
- const PASS_KEY: &str = "";
- //ws-订阅公共频道信息
- #[tokio::test(flavor = "multi_thread", worker_threads = 5)]
- async fn ws_custom_subscribe_pu() {
- global::log_utils::init_log_with_trace();
- let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
- let (read_tx, mut read_rx) = futures_channel::mpsc::unbounded();
- let mut ws = get_ws(None, KucoinSwapWsType::Public).await;
- ws.set_symbols(vec!["xbt_usdtM".to_string()]);
- ws.set_subscribe(vec![
- KucoinSwapSubscribeType::PuContractMarketLevel2Depth50,
- KucoinSwapSubscribeType::PuContractMarketExecution,
- KucoinSwapSubscribeType::PuContractMarkettickerV2,
- ]);
- 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 {
- if let Some(data) = read_rx.next().await {
- trace!("读取数据data:{:?}",data)
- }
- }
- });
- //写数据
- let _bool_v2_clone = Arc::clone(&is_shutdown_arc);
- let _write_tx_clone = Arc::clone(&write_tx_am);
- // 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 close_message = Message::Close(Some(close_frame));
- // // AbstractWsMode::send_subscribe(write_tx_clone.clone(), Message::Text("32313221".to_string()));
- // AbstractWsMode::send_subscribe(write_tx_clone.clone(), close_message);
- // trace!("发送指令成功");
- // }
- // trace!("线程-数据写入-结束");
- // });
- // loop {
- let t1 = tokio::spawn(async move {
- //链接
- let bool_v3_clone = Arc::clone(&is_shutdown_arc);
- ws.ws_connect_async(bool_v3_clone, &write_tx_am, write_rx, read_tx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
- trace!("test 唯一线程结束--");
- });
- tokio::try_join!(t1).unwrap();
- trace!("当此结束");
- // }
- trace!("重启!");
- trace!("参考交易所关闭");
- return;
- }
- //ws-订阅私有频道信息
- #[tokio::test(flavor = "multi_thread", worker_threads = 5)]
- async fn ws_custom_subscribe_pr() {
- global::log_utils::init_log_with_trace();
- //对象
- let btree_map = KucoinSwapLogin {
- access_key: ACCESS_KEY.to_string(),
- secret_key: SECRET_KEY.to_string(),
- pass_key: PASS_KEY.to_string(),
- };
- let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
- let (read_tx, mut read_rx) = futures_channel::mpsc::unbounded();
- // let (write_tx, write_rx) = tokio::sync::broadcast::channel::<Message>(10);
- // let (read_tx, mut read_rx) = tokio::sync::broadcast::channel::<ResponseData>(10);
- let mut ws = get_ws(Option::from(btree_map), KucoinSwapWsType::Private).await;
- ws.set_symbols(vec!["xbt_usdtM".to_string()]);
- ws.set_subscribe(vec![
- // KucoinSwapSubscribeType::PuContractMarketLevel2Depth50,
- // KucoinSwapSubscribeType::PuContractMarketExecution,
- KucoinSwapSubscribeType::PuContractMarkettickerV2,
- KucoinSwapSubscribeType::PrContractAccountWallet,
- KucoinSwapSubscribeType::PrContractPosition,
- KucoinSwapSubscribeType::PrContractMarketTradeOrdersSys,
- KucoinSwapSubscribeType::PrContractMarketTradeOrders,
- ]);
- 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 {
- if let Some(data) = read_rx.next().await {
- trace!("读取数据data:{:?}",data)
- }
- }
- });
- //写数据
- let _bool_v2_clone = Arc::clone(&is_shutdown_arc);
- let _write_tx_clone = Arc::clone(&write_tx_am);
- // 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 close_message = Message::Close(Some(close_frame));
- // // AbstractWsMode::send_subscribe(write_tx_clone.clone(), Message::Text("32313221".to_string()));
- // AbstractWsMode::send_subscribe(write_tx_clone.clone(), close_message);
- // trace!("发送指令成功");
- // }
- // trace!("线程-数据写入-结束");
- // });
- // loop {
- let t1 = tokio::spawn(async move {
- //链接
- let bool_v3_clone = Arc::clone(&is_shutdown_arc);
- ws.ws_connect_async(bool_v3_clone, &write_tx_am, write_rx, read_tx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
- trace!("test 唯一线程结束--");
- });
- tokio::try_join!(t1).unwrap();
- trace!("当此结束");
- // }
- trace!("重启!");
- trace!("参考交易所关闭");
- return;
- }
- async fn get_ws(btree_map: Option<KucoinSwapLogin>, type_v: KucoinSwapWsType) -> KucoinSwapWs {
- let ku_ws = KucoinSwapWs::new(false, btree_map,
- type_v).await;
- ku_ws
- }
|