|
@@ -19,6 +19,7 @@ pub enum BitgetSwapWsType {
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
|
pub enum BitgetSwapSubscribeType {
|
|
pub enum BitgetSwapSubscribeType {
|
|
|
PuTrade,
|
|
PuTrade,
|
|
|
|
|
+ PuBooks1,
|
|
|
|
|
|
|
|
PrAccount,
|
|
PrAccount,
|
|
|
PrPosition,
|
|
PrPosition,
|
|
@@ -35,12 +36,12 @@ pub struct BitgetSwapLogin {
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
|
pub struct BitgetSwapWs {
|
|
pub struct BitgetSwapWs {
|
|
|
- label: String, // 类型
|
|
|
|
|
- address_url: String, // 地址
|
|
|
|
|
- login_param: Option<BitgetSwapLogin>, // 账号
|
|
|
|
|
- symbol_s: Vec<String>, // 币对
|
|
|
|
|
- subscribe_types: Vec<BitgetSwapSubscribeType>, // 订阅
|
|
|
|
|
- heartbeat_time: u64, // 心跳间隔
|
|
|
|
|
|
|
+ label: String, // 类型
|
|
|
|
|
+ address_url: String, // 地址
|
|
|
|
|
+ login_param: Option<BitgetSwapLogin>, // 账号
|
|
|
|
|
+ symbol_s: Vec<String>, // 币对
|
|
|
|
|
+ subscribe_types: Vec<BitgetSwapSubscribeType>, // 订阅
|
|
|
|
|
+ heartbeat_time: u64, // 心跳间隔
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
impl BitgetSwapWs {
|
|
impl BitgetSwapWs {
|
|
@@ -70,7 +71,7 @@ impl BitgetSwapWs {
|
|
|
login_param,
|
|
login_param,
|
|
|
symbol_s: vec![],
|
|
symbol_s: vec![],
|
|
|
subscribe_types: vec![],
|
|
subscribe_types: vec![],
|
|
|
- heartbeat_time: 1000 * 20,
|
|
|
|
|
|
|
+ heartbeat_time: 1000 * 20
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -96,6 +97,7 @@ impl BitgetSwapWs {
|
|
|
for t in self.subscribe_types.clone() {
|
|
for t in self.subscribe_types.clone() {
|
|
|
if match t {
|
|
if match t {
|
|
|
BitgetSwapSubscribeType::PuTrade => false,
|
|
BitgetSwapSubscribeType::PuTrade => false,
|
|
|
|
|
+ BitgetSwapSubscribeType::PuBooks1 => false,
|
|
|
|
|
|
|
|
BitgetSwapSubscribeType::PrAccount => true,
|
|
BitgetSwapSubscribeType::PrAccount => true,
|
|
|
BitgetSwapSubscribeType::PrOrders => true,
|
|
BitgetSwapSubscribeType::PrOrders => true,
|
|
@@ -121,6 +123,13 @@ impl BitgetSwapWs {
|
|
|
"instId": symbol,
|
|
"instId": symbol,
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
|
|
+ BitgetSwapSubscribeType::PuBooks1 => {
|
|
|
|
|
+ json!({
|
|
|
|
|
+ "instType": "USDT-FUTURES",
|
|
|
|
|
+ "channel": "books1",
|
|
|
|
|
+ "instId": symbol,
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
|
|
|
// 私有订阅
|
|
// 私有订阅
|
|
|
BitgetSwapSubscribeType::PrAccount => {
|
|
BitgetSwapSubscribeType::PrAccount => {
|
|
@@ -188,14 +197,14 @@ impl BitgetSwapWs {
|
|
|
let sign = base64::encode(result);
|
|
let sign = base64::encode(result);
|
|
|
|
|
|
|
|
let login_json = json!({
|
|
let login_json = json!({
|
|
|
- "op": "login",
|
|
|
|
|
- "args": [{
|
|
|
|
|
- "apiKey": access_key,
|
|
|
|
|
- "passphrase": passphrase,
|
|
|
|
|
- "timestamp": timestamp,
|
|
|
|
|
- "sign": sign
|
|
|
|
|
- }]
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ "op": "login",
|
|
|
|
|
+ "args": [{
|
|
|
|
|
+ "apiKey": access_key,
|
|
|
|
|
+ "passphrase": passphrase,
|
|
|
|
|
+ "timestamp": timestamp,
|
|
|
|
|
+ "sign": sign
|
|
|
|
|
+ }]
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
info!("---login_json: {0}", login_json.to_string());
|
|
info!("---login_json: {0}", login_json.to_string());
|
|
|
info!("---登陆: {}", login_json.to_string());
|
|
info!("---登陆: {}", login_json.to_string());
|
|
@@ -217,11 +226,22 @@ impl BitgetSwapWs {
|
|
|
Future: std::future::Future<Output=()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
|
|
Future: std::future::Future<Output=()> + Send + 'static, // 确保 Fut 是一个 Future,且输出类型为 ()
|
|
|
{
|
|
{
|
|
|
let login_is = self.contains_pr();
|
|
let login_is = self.contains_pr();
|
|
|
- let subscription = self.get_subscription();
|
|
|
|
|
let address_url = self.address_url.clone();
|
|
let address_url = self.address_url.clone();
|
|
|
let label = self.label.clone();
|
|
let label = self.label.clone();
|
|
|
let heartbeat_time = self.heartbeat_time.clone();
|
|
let heartbeat_time = self.heartbeat_time.clone();
|
|
|
|
|
|
|
|
|
|
+ // 登录相关
|
|
|
|
|
+ if login_is {
|
|
|
|
|
+ let login_str = self.log_in_to_str();
|
|
|
|
|
+ info!("发起ws登录: {}", login_str);
|
|
|
|
|
+ // TODO 这样写不能断线重连,后面再想办法修吧
|
|
|
|
|
+ let write_tx_clone2 = Arc::clone(write_tx_am);
|
|
|
|
|
+ AbstractWsMode::send_subscribe(write_tx_clone2, Message::Text(login_str)).await;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 设置订阅
|
|
|
|
|
+ let subscription = self.get_subscription();
|
|
|
|
|
+ let mut subscribe_array = vec![subscription.to_string()];
|
|
|
|
|
+ info!(?subscribe_array);
|
|
|
|
|
|
|
|
//心跳-- 方法内部线程启动
|
|
//心跳-- 方法内部线程启动
|
|
|
let write_tx_clone1 = Arc::clone(write_tx_am);
|
|
let write_tx_clone1 = Arc::clone(write_tx_am);
|
|
@@ -229,26 +249,16 @@ impl BitgetSwapWs {
|
|
|
AbstractWsMode::ping_or_pong(write_tx_clone1, HeartbeatType::Ping, heartbeat_time).await;
|
|
AbstractWsMode::ping_or_pong(write_tx_clone1, HeartbeatType::Ping, heartbeat_time).await;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- //设置订阅
|
|
|
|
|
- let mut subscribe_array = vec![];
|
|
|
|
|
- if login_is {
|
|
|
|
|
- //登录相关
|
|
|
|
|
- let login_str = self.log_in_to_str();
|
|
|
|
|
- let write_tx_clone2 = Arc::clone(write_tx_am);
|
|
|
|
|
- AbstractWsMode::send_subscribe(write_tx_clone2, Message::Text(login_str)).await;
|
|
|
|
|
- tokio::time::sleep(Duration::from_millis(1000 * 3)).await;
|
|
|
|
|
- }
|
|
|
|
|
- subscribe_array.push(subscription.to_string());
|
|
|
|
|
-
|
|
|
|
|
//链接
|
|
//链接
|
|
|
let t2 = tokio::spawn(async move {
|
|
let t2 = tokio::spawn(async move {
|
|
|
let write_to_socket_rx_arc = Arc::new(Mutex::new(write_to_socket_rx));
|
|
let write_to_socket_rx_arc = Arc::new(Mutex::new(write_to_socket_rx));
|
|
|
|
|
|
|
|
loop {
|
|
loop {
|
|
|
info!("bitget_usdt_swap socket 连接中……");
|
|
info!("bitget_usdt_swap socket 连接中……");
|
|
|
|
|
+
|
|
|
// ws层重连
|
|
// ws层重连
|
|
|
AbstractWsMode::ws_connect_async(is_shutdown_arc.clone(), handle_function.clone(), address_url.clone(),
|
|
AbstractWsMode::ws_connect_async(is_shutdown_arc.clone(), handle_function.clone(), address_url.clone(),
|
|
|
- label.clone(), subscribe_array.clone(), write_to_socket_rx_arc.clone(),
|
|
|
|
|
|
|
+ login_is, label.clone(), subscribe_array.clone(), write_to_socket_rx_arc.clone(),
|
|
|
Self::message_text, Self::message_ping, Self::message_pong).await;
|
|
Self::message_text, Self::message_ping, Self::message_pong).await;
|
|
|
|
|
|
|
|
error!("bitget_usdt_swap socket 断连,1s以后重连……");
|
|
error!("bitget_usdt_swap socket 断连,1s以后重连……");
|
|
@@ -279,10 +289,10 @@ impl BitgetSwapWs {
|
|
|
}
|
|
}
|
|
|
//数据解析
|
|
//数据解析
|
|
|
pub fn ok_text(text: String) -> ResponseData {
|
|
pub fn ok_text(text: String) -> ResponseData {
|
|
|
- let mut res_data = ResponseData::new("".to_string(), "200".to_string(), "success".to_string(), Value::Null);
|
|
|
|
|
|
|
+ let mut res_data = ResponseData::new("".to_string(), "200".to_string(), text.clone(), Value::Null);
|
|
|
let json_value: Value = serde_json::from_str(&text).unwrap();
|
|
let json_value: Value = serde_json::from_str(&text).unwrap();
|
|
|
|
|
|
|
|
- if json_value.get("event").is_some() && json_value["event"].as_str() == Option::from("login") {
|
|
|
|
|
|
|
+ if json_value.get("event").is_some() && json_value["event"].as_str() == Some("login") {
|
|
|
if json_value.get("code").is_some() && json_value["code"] == 0 {
|
|
if json_value.get("code").is_some() && json_value["code"] == 0 {
|
|
|
res_data.message = "登陆成功".to_string();
|
|
res_data.message = "登陆成功".to_string();
|
|
|
} else {
|
|
} else {
|
|
@@ -290,11 +300,14 @@ impl BitgetSwapWs {
|
|
|
}
|
|
}
|
|
|
res_data.channel = "login".to_string();
|
|
res_data.channel = "login".to_string();
|
|
|
res_data.code = "-200".to_string();
|
|
res_data.code = "-200".to_string();
|
|
|
|
|
+ res_data.data = json_value;
|
|
|
|
|
+
|
|
|
res_data
|
|
res_data
|
|
|
- } else if json_value.get("event").is_some() && json_value["event"].as_str() == Option::from("subscribe") {
|
|
|
|
|
|
|
+ } else if json_value.get("event").is_some() && json_value["event"].as_str() == Some("subscribe") {
|
|
|
res_data.code = "-201".to_string();
|
|
res_data.code = "-201".to_string();
|
|
|
res_data.data = json_value.clone();
|
|
res_data.data = json_value.clone();
|
|
|
res_data.channel = format!("{}", json_value["arg"]["channel"].as_str().unwrap());
|
|
res_data.channel = format!("{}", json_value["arg"]["channel"].as_str().unwrap());
|
|
|
|
|
+ res_data.message = "success".to_string();
|
|
|
res_data
|
|
res_data
|
|
|
} else if json_value.get("action").is_some() {
|
|
} else if json_value.get("action").is_some() {
|
|
|
res_data.data = json_value["data"].clone();
|
|
res_data.data = json_value["data"].clone();
|
|
@@ -303,6 +316,7 @@ impl BitgetSwapWs {
|
|
|
} else {
|
|
} else {
|
|
|
res_data.code = "200".to_string();
|
|
res_data.code = "200".to_string();
|
|
|
}
|
|
}
|
|
|
|
|
+ res_data.message = "success".to_string();
|
|
|
res_data.channel = format!("{}", json_value["arg"]["channel"].as_str().unwrap());
|
|
res_data.channel = format!("{}", json_value["arg"]["channel"].as_str().unwrap());
|
|
|
res_data.reach_time = json_value["ts"].as_i64().unwrap() * 1000;
|
|
res_data.reach_time = json_value["ts"].as_i64().unwrap() * 1000;
|
|
|
res_data
|
|
res_data
|