|
@@ -20,6 +20,12 @@ pub enum MexcSpotWsType {
|
|
|
PublicAndPrivate,
|
|
PublicAndPrivate,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+pub mod mexc_spot {
|
|
|
|
|
+ include!(concat!(env!("OUT_DIR"), "/_.rs"));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+use mexc_spot::PublicIncreaseDepthsV3Api; // 使用 proto 中定义的消息名
|
|
|
|
|
+use mexc_spot::PublicSpotKlineV3Api; // 假设 KlineData 是 kline.proto 中定义的消息名
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
#[derive(Debug)]
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
@@ -83,7 +89,7 @@ impl MexcSpotWs {
|
|
|
/*******公共频道-私有频道数据组装*/
|
|
/*******公共频道-私有频道数据组装*/
|
|
|
let address_url = match ws_type {
|
|
let address_url = match ws_type {
|
|
|
MexcSpotWsType::PublicAndPrivate => {
|
|
MexcSpotWsType::PublicAndPrivate => {
|
|
|
- let url = "ws://wbs-api.mexc.com/ws".to_string();
|
|
|
|
|
|
|
+ let url = "wss://wbs-api.mexc.com/ws".to_string();
|
|
|
url
|
|
url
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
@@ -119,7 +125,7 @@ impl MexcSpotWs {
|
|
|
// 大写
|
|
// 大写
|
|
|
*symbol = symbol.to_uppercase();
|
|
*symbol = symbol.to_uppercase();
|
|
|
// 字符串替换
|
|
// 字符串替换
|
|
|
- *symbol = symbol.replace("_", "_");
|
|
|
|
|
|
|
+ *symbol = symbol.replace("_", "");
|
|
|
}
|
|
}
|
|
|
self.symbol_s = symbol_array;
|
|
self.symbol_s = symbol_array;
|
|
|
}
|
|
}
|
|
@@ -142,7 +148,7 @@ impl MexcSpotWs {
|
|
|
MexcSpotWsSubscribeType::PuFuturesDepth => {
|
|
MexcSpotWsSubscribeType::PuFuturesDepth => {
|
|
|
json!({
|
|
json!({
|
|
|
"method": "SUBSCRIPTION",
|
|
"method": "SUBSCRIPTION",
|
|
|
- "param": [
|
|
|
|
|
|
|
+ "params": [
|
|
|
format!("spot@public.aggre.depth.v3.api.pb@10ms@{symbol}")
|
|
format!("spot@public.aggre.depth.v3.api.pb@10ms@{symbol}")
|
|
|
]
|
|
]
|
|
|
})
|
|
})
|
|
@@ -151,7 +157,7 @@ impl MexcSpotWs {
|
|
|
MexcSpotWsSubscribeType::PuFuturesRecords(interval) => {
|
|
MexcSpotWsSubscribeType::PuFuturesRecords(interval) => {
|
|
|
json!({
|
|
json!({
|
|
|
"method": "SUBSCRIPTION",
|
|
"method": "SUBSCRIPTION",
|
|
|
- "param": [
|
|
|
|
|
|
|
+ "params": [
|
|
|
format!("spot@public.kline.v3.api.pb@{symbol}@{interval}")
|
|
format!("spot@public.kline.v3.api.pb@{symbol}@{interval}")
|
|
|
]
|
|
]
|
|
|
})
|
|
})
|
|
@@ -237,34 +243,26 @@ impl MexcSpotWs {
|
|
|
}
|
|
}
|
|
|
//数据解析-二进制
|
|
//数据解析-二进制
|
|
|
pub fn message_binary(po: Vec<u8>) -> Option<Response> {
|
|
pub fn message_binary(po: Vec<u8>) -> Option<Response> {
|
|
|
|
|
+ info!("{:?}", po);
|
|
|
//二进制WebSocket消息
|
|
//二进制WebSocket消息
|
|
|
- // let message_str = format!("Binary:{:?}", _po);
|
|
|
|
|
- // Option::from(ResponseData::new("".to_string(), 2, message_str, Value::Null))
|
|
|
|
|
- // let result = String::from_utf8(bytes);
|
|
|
|
|
- // let result = String::from_utf8(po);
|
|
|
|
|
-
|
|
|
|
|
- let mut gz_decoder = GzDecoder::new(&po[..]);
|
|
|
|
|
- let mut decompressed_data = Vec::new();
|
|
|
|
|
-
|
|
|
|
|
- // 尝试解压数据
|
|
|
|
|
- if let Ok(_) = gz_decoder.read_to_end(&mut decompressed_data) {
|
|
|
|
|
- // 将解压后的字节向量转换为 UTF-8 字符串
|
|
|
|
|
- match String::from_utf8(decompressed_data) {
|
|
|
|
|
- Ok(text) => {
|
|
|
|
|
- let response_data = Self::ok_text(text);
|
|
|
|
|
- return Option::from(response_data);
|
|
|
|
|
- }
|
|
|
|
|
- Err(_) => {
|
|
|
|
|
- return Option::from(Response::new("".to_string(), 400, "二进制数据转化出错".to_string(), Value::Null));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // let mut gz_decoder = GzDecoder::new(&po[..]);
|
|
|
|
|
+ // let mut decompressed_data = Vec::new();
|
|
|
|
|
+
|
|
|
|
|
+ // 将解压后的字节向量转换为 UTF-8 字符串
|
|
|
|
|
+ match String::from_utf8(po) {
|
|
|
|
|
+ Ok(text) => {
|
|
|
|
|
+ let response_data = Self::ok_text(text);
|
|
|
|
|
+ Option::from(response_data)
|
|
|
|
|
+ }
|
|
|
|
|
+ Err(e) => {
|
|
|
|
|
+ Option::from(Response::new("".to_string(), 400, "二进制数据转化出错".to_string(), Value::Null))
|
|
|
}
|
|
}
|
|
|
- } else {
|
|
|
|
|
- return Option::from(Response::new("".to_string(), 400, "二进制数据转化出错".to_string(), Value::Null));
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
//数据解析
|
|
//数据解析
|
|
|
pub fn ok_text(text: String) -> Response
|
|
pub fn ok_text(text: String) -> Response
|
|
|
{
|
|
{
|
|
|
|
|
+ info!("{}", text);
|
|
|
let mut res_data = Response::new("".to_string(), 200, "success".to_string(), Value::Null);
|
|
let mut res_data = Response::new("".to_string(), 200, "success".to_string(), Value::Null);
|
|
|
let json_value: Value = serde_json::from_str(&text).unwrap();
|
|
let json_value: Value = serde_json::from_str(&text).unwrap();
|
|
|
|
|
|
|
@@ -308,6 +306,7 @@ impl MexcSpotWs {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
None => {
|
|
None => {
|
|
|
|
|
+ res_data.data = json_value.clone();
|
|
|
res_data.code = -1;
|
|
res_data.code = -1;
|
|
|
res_data.message = "未知解析".to_string();
|
|
res_data.message = "未知解析".to_string();
|
|
|
}
|
|
}
|
|
@@ -337,10 +336,11 @@ mod tests {
|
|
|
let mut ws = MexcSpotWs::new_with_tag("Mexc".to_string(), None, MexcSpotWsType::PublicAndPrivate);
|
|
let mut ws = MexcSpotWs::new_with_tag("Mexc".to_string(), None, MexcSpotWsType::PublicAndPrivate);
|
|
|
|
|
|
|
|
ws.set_subscribe(vec![
|
|
ws.set_subscribe(vec![
|
|
|
- MexcSpotWsSubscribeType::PuFuturesRecords("Min1".to_string())
|
|
|
|
|
|
|
+ // MexcSpotWsSubscribeType::PuFuturesRecords("Min1".to_string()),
|
|
|
|
|
+ MexcSpotWsSubscribeType::PuFuturesDepth
|
|
|
]);
|
|
]);
|
|
|
|
|
|
|
|
- ws.set_symbols(vec!["BTCUSDT".to_string()]);
|
|
|
|
|
|
|
+ ws.set_symbols(vec!["BTC_USDT".to_string()]);
|
|
|
|
|
|
|
|
let fun = move |response: Response| {
|
|
let fun = move |response: Response| {
|
|
|
info!("{}", serde_json::to_string_pretty(&response.data).unwrap());
|
|
info!("{}", serde_json::to_string_pretty(&response.data).unwrap());
|