|
|
@@ -1,9 +1,10 @@
|
|
|
use std::{fs, thread};
|
|
|
use exchanges::gate_swap_rest::GateSwapRest;
|
|
|
use std::collections::BTreeMap;
|
|
|
-use std::io::{BufRead, BufReader, Write};
|
|
|
-use std::net::{TcpListener, TcpStream};
|
|
|
+use std::io::{BufRead, BufReader};
|
|
|
+use std::net::{TcpListener};
|
|
|
use std::sync::{mpsc};
|
|
|
+use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
use exchanges::binance_usdt_swap_rest::BinanceUsdtSwapRest;
|
|
|
use exchanges::kucoin_swap_rest::KucoinSwapRest;
|
|
|
use exchanges::kuconin_swap_ws::{KuconinSubscribeType, KuconinSwapWs, KuconinWsType};
|
|
|
@@ -11,6 +12,9 @@ use exchanges::{proxy};
|
|
|
use exchanges::binance_usdt_swap_ws::BinanceUsdtSwapWs;
|
|
|
use exchanges::okx_swap_ws::{OkxSubscribeType, OkxSwapWs, OkxWsType};
|
|
|
|
|
|
+use std::io::{Read, Write};
|
|
|
+use std::net::{TcpStream, ToSocketAddrs};
|
|
|
+
|
|
|
|
|
|
#[tokio::test]
|
|
|
async fn test_import() {
|
|
|
@@ -38,16 +42,71 @@ async fn test_import() {
|
|
|
//Kuconin-ws--公共频道
|
|
|
// demo_ws_kucoin_pu().await;
|
|
|
//Kuconin-ws--私有频道
|
|
|
- demo_ws_kucoin_pr().await;
|
|
|
+ // demo_ws_kucoin_pr().await;
|
|
|
|
|
|
//okx - Business 频道
|
|
|
// demo_ws_okx_bu().await;
|
|
|
//okx - public 频道
|
|
|
// demo_ws_okx_pu().await;
|
|
|
+
|
|
|
+ demo_so();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ fn demo_so() {
|
|
|
+ // 代理服务器地址和端口
|
|
|
+ // let proxy_address = "127.0.0.1:7890";
|
|
|
+ // // 目标服务器地址和端口
|
|
|
+ // let target_address = "wss://ws.okx.com:8443/ws/v5/public";
|
|
|
+ //
|
|
|
+ // // 建立与代理服务器的连接
|
|
|
+ // let mut proxy_stream = TcpStream::connect(proxy_address).expect("Failed to connect to proxy");
|
|
|
+ //
|
|
|
+ // // 发送代理请求
|
|
|
+ // let request = format!("CONNECT {}\r\n\r\n", target_address);
|
|
|
+ // proxy_stream.write_all(request.as_bytes()).expect("Failed to send proxy request");
|
|
|
+ //
|
|
|
+ // // 读取代理响应
|
|
|
+ // let mut response = String::new();
|
|
|
+ // proxy_stream.read_to_string(&mut response).expect("Failed to read proxy response");
|
|
|
+ //
|
|
|
+ // // 检查代理响应是否成功
|
|
|
+ // if !response.contains("200 Connection established") {
|
|
|
+ // println!("Proxy connection failed: {}", response);
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // // 从代理连接中获取原始 TCP 流
|
|
|
+ // let mut tcp_stream = std::mem::ManuallyDrop::new(proxy_stream);
|
|
|
+ //
|
|
|
+ // // 现在你可以使用 `tcp_stream` 来进行与目标服务器的通信
|
|
|
+ // // 例如,发送和接收数据
|
|
|
+ // // tcp_stream.write_all(b"Hello, server!").expect("Failed to send data");
|
|
|
+ //
|
|
|
+ // thread::spawn(move || {
|
|
|
+ // loop {
|
|
|
+ // let mut buffer = [0u8; 1024]; // 用于存储读取的数据的缓冲区
|
|
|
+ // let bytes_read = tcp_stream.read(&mut buffer).expect("Failed to read data");
|
|
|
+ //
|
|
|
+ // // 将读取的数据转换为字符串并打印
|
|
|
+ // if let Ok(data) = std::str::from_utf8(&buffer[..bytes_read]) {
|
|
|
+ // println!("Received data: {}", data);
|
|
|
+ // } else {
|
|
|
+ // println!("Received data contains non-UTF8 characters");
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // // 最后记得手动释放套接字
|
|
|
+ // // 注意:不要调用 `drop(tcp_stream)`,因为我们使用了 `ManuallyDrop` 来避免自动释放
|
|
|
+ // unsafe {
|
|
|
+ // std::mem::ManuallyDrop::drop(&mut tcp_stream);
|
|
|
+ // }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
async fn demo_ws_okx_pu() {
|
|
|
- let btree_map: BTreeMap<String, String> = BTreeMap::new();
|
|
|
+ let btree_map: BTreeMap<String, String> = BTreeMap::new();
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
let mut ku_ws = OkxSwapWs::new(false, btree_map, OkxWsType::Public, tx).await;
|
|
|
ku_ws.set_subscribe(vec![OkxSubscribeType::PuIndexTickers]);
|
|
|
@@ -100,7 +159,7 @@ async fn demo_ws_kucoin_pr() {
|
|
|
}
|
|
|
|
|
|
async fn demo_ws_kucoin_pu() {
|
|
|
- let btree_map: BTreeMap<String, String> = BTreeMap::new();
|
|
|
+ let btree_map: BTreeMap<String, String> = BTreeMap::new();
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
let mut ku_ws = KuconinSwapWs::new(false, btree_map, KuconinWsType::Public, tx).await;
|
|
|
ku_ws.set_subscribe(vec![KuconinSubscribeType::PuContractMarketLevel2Depth50]);
|
|
|
@@ -255,7 +314,7 @@ fn demo_http() {
|
|
|
for stream in listener.incoming() {
|
|
|
let stream = stream.unwrap();
|
|
|
|
|
|
- handle_connection(stream);
|
|
|
+ handle_connection(TcpStream::try_from(stream).unwrap());
|
|
|
}
|
|
|
}
|
|
|
|