binance_spot_test.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. use std::sync::Arc;
  2. use std::sync::atomic::AtomicBool;
  3. use std::time::Duration;
  4. use futures_util::StreamExt;
  5. use tokio::sync::Mutex;
  6. use tokio_tungstenite::tungstenite::Message;
  7. use tracing::trace;
  8. use exchanges::binance_spot_ws::{BinanceSpotLogin, BinanceSpotSubscribeType, BinanceSpotWs, BinanceSpotWsType};
  9. use exchanges::socket_tool::AbstractWsMode;
  10. // 账号密码
  11. const ACCESS_KEY: &str = "";
  12. const SECRET_KEY: &str = "";
  13. //ws-订阅公共频道信息
  14. #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
  15. async fn ws_custom_subscribe() {
  16. global::log_utils::init_log_with_trace();
  17. let (write_tx, write_rx) = futures_channel::mpsc::unbounded();
  18. let (read_tx, mut read_rx) = futures_channel::mpsc::unbounded();
  19. let login_param = BinanceSpotLogin {
  20. api_key: ACCESS_KEY.to_string(),
  21. api_secret: SECRET_KEY.to_string(),
  22. };
  23. let mut ws = get_ws(None);
  24. ws.set_symbols(vec!["BTC_USDT".to_string()]);
  25. ws.set_subscribe(vec![
  26. // BinanceSpotSubscribeType::PuBookTicker,
  27. // BinanceSpotSubscribeType::PuAggTrade,
  28. BinanceSpotSubscribeType::PuDepth20levels100ms,
  29. ]);
  30. let write_tx_am = Arc::new(Mutex::new(write_tx));
  31. let is_shutdown_arc = Arc::new(AtomicBool::new(true));
  32. //读取
  33. let _is_shutdown_arc_clone = Arc::clone(&is_shutdown_arc);
  34. let _tr = tokio::spawn(async move {
  35. trace!("线程-数据读取-开启");
  36. loop {
  37. if let Some(data) = read_rx.next().await {
  38. trace!("读取数据data:{:?}",data)
  39. }
  40. }
  41. // trace!("线程-数据读取-结束");
  42. });
  43. //写数据
  44. // let bool_v2_clone = Arc::clone(&is_shutdown_arc);
  45. // let write_tx_clone = Arc::clone(&write_tx_am);
  46. // let su = ws.get_subscription();
  47. // let tw = tokio::spawn(async move {
  48. // trace!("线程-数据写入-开始");
  49. // loop {
  50. // tokio::time::sleep(Duration::from_millis(20 * 1000)).await;
  51. // // let close_frame = CloseFrame {
  52. // // code: CloseCode::Normal,
  53. // // reason: Cow::Borrowed("Bye bye"),
  54. // // };
  55. // // let message = Message::Close(Some(close_frame));
  56. //
  57. //
  58. // let message = Message::Text(su.clone());
  59. // AbstractWsMode::send_subscribe(write_tx_clone.clone(), message.clone()).await;
  60. // trace!("发送指令成功");
  61. // }
  62. // trace!("线程-数据写入-结束");
  63. // });
  64. let t1 = tokio::spawn(async move {
  65. //链接
  66. let bool_v3_clone = Arc::clone(&is_shutdown_arc);
  67. ws.ws_connect_async(bool_v3_clone, &write_tx_am, write_rx, read_tx).await.expect("链接失败(内部一个心跳线程应该已经关闭了)");
  68. trace!("test 唯一线程结束--");
  69. });
  70. tokio::try_join!(t1).unwrap();
  71. trace!("当此结束");
  72. trace!("重启!");
  73. trace!("参考交易所关闭");
  74. return;
  75. }
  76. fn get_ws(login_param: Option<BinanceSpotLogin>) -> BinanceSpotWs {
  77. let binance_ws = BinanceSpotWs::new(false,
  78. login_param, BinanceSpotWsType::PublicAndPrivate,
  79. );
  80. binance_ws
  81. }