|
|
@@ -1,5 +1,6 @@
|
|
|
use std::sync::Arc;
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
+use std::time::Duration;
|
|
|
|
|
|
use chrono::Utc;
|
|
|
use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
|
|
|
@@ -191,7 +192,7 @@ impl BybitSwapWs {
|
|
|
is_shutdown_arc: Arc<AtomicBool>,
|
|
|
handle_function: F,
|
|
|
write_tx_am: &Arc<Mutex<UnboundedSender<Message>>>,
|
|
|
- write_rx: UnboundedReceiver<Message>) -> Result<(), Error>
|
|
|
+ write_to_socket_rx: UnboundedReceiver<Message>) -> Result<(), Error>
|
|
|
where
|
|
|
F: Fn(ResponseData) -> Future + Clone + Send + 'static + Sync,
|
|
|
Future: std::future::Future<Output=()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
|
|
|
@@ -220,40 +221,39 @@ impl BybitSwapWs {
|
|
|
trace!("线程-异步心跳-结束");
|
|
|
});
|
|
|
|
|
|
- //设置订阅
|
|
|
- let mut subscribe_array = vec![];
|
|
|
- if login_is {
|
|
|
- let expires = timestamp + 1000;
|
|
|
- let message = format!("GET/realtime{}", expires);
|
|
|
+ //链接
|
|
|
+ let t2 = tokio::spawn(async move {
|
|
|
+ let write_to_socket_rx_arc = Arc::new(Mutex::new(write_to_socket_rx));
|
|
|
|
|
|
- let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_bytes());
|
|
|
- let result = hmac::sign(&hmac_key, &message.as_bytes());
|
|
|
- let signature = hex::encode(result.as_ref());
|
|
|
+ loop {
|
|
|
+ info!("bybit_usdt_swap socket 连接中……");
|
|
|
|
|
|
- //登录相关
|
|
|
- let str = json!({
|
|
|
- "op": "auth",
|
|
|
- "args": [api_key, expires, signature]
|
|
|
- });
|
|
|
- subscribe_array.push(str.to_string());
|
|
|
- }
|
|
|
- subscribe_array.push(subscription.to_string());
|
|
|
+ //设置订阅
|
|
|
+ let mut subscribe_array = vec![];
|
|
|
+ if login_is {
|
|
|
+ let expires = timestamp + 1000;
|
|
|
+ let message = format!("GET/realtime{}", expires);
|
|
|
|
|
|
- //链接
|
|
|
- let t2 = tokio::spawn(async move {
|
|
|
- trace!("线程-异步链接-开始");
|
|
|
- match AbstractWsMode::ws_connect_async(is_shutdown_arc,
|
|
|
- handle_function,
|
|
|
- address_url.clone(),
|
|
|
- label.clone(),
|
|
|
- subscribe_array,
|
|
|
- write_rx,
|
|
|
- Self::message_text,
|
|
|
- Self::message_ping,
|
|
|
- Self::message_pong,
|
|
|
- ).await {
|
|
|
- Ok(_) => { trace!("线程-异步链接-结束"); }
|
|
|
- Err(e) => { error!("发生异常:bybit_usdt_swap 链接关闭-{:?}",e); }
|
|
|
+ let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, secret_key.as_bytes());
|
|
|
+ let result = hmac::sign(&hmac_key, &message.as_bytes());
|
|
|
+ let signature = hex::encode(result.as_ref());
|
|
|
+
|
|
|
+ //登录相关
|
|
|
+ let str = json!({
|
|
|
+ "op": "auth",
|
|
|
+ "args": [api_key, expires, signature]
|
|
|
+ });
|
|
|
+ subscribe_array.push(str.to_string());
|
|
|
+ }
|
|
|
+ subscribe_array.push(subscription.to_string());
|
|
|
+
|
|
|
+ // ws网络层重连
|
|
|
+ AbstractWsMode::ws_connect_async(is_shutdown_arc.clone(), handle_function.clone(), address_url.clone(),
|
|
|
+ label.clone(), subscribe_array, write_to_socket_rx_arc.clone(),
|
|
|
+ Self::message_text, Self::message_ping, Self::message_pong).await;
|
|
|
+
|
|
|
+ error!("bybit_usdt_swap socket 断连,1s以后重连……");
|
|
|
+ tokio::time::sleep(Duration::from_secs(1)).await;
|
|
|
}
|
|
|
});
|
|
|
tokio::try_join!(t2).unwrap();
|