-
Notifications
You must be signed in to change notification settings - Fork 24
/
bench_tungstenite.rs
45 lines (42 loc) · 1.28 KB
/
bench_tungstenite.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use clap::Parser;
use tracing_subscriber::util::SubscriberInitExt;
/// websocket client connect to binance futures websocket
#[derive(Parser)]
struct Args {
/// server host
#[arg(long, default_value = "127.0.0.1")]
host: String,
/// server port
#[arg(short, long, default_value = "9000")]
port: u16,
/// level
#[arg(short, long, default_value = "info")]
level: tracing::Level,
}
fn main() -> Result<(), ()> {
let args = Args::parse();
tracing_subscriber::fmt::fmt()
.with_max_level(args.level)
.finish()
.try_init()
.expect("failed to init log");
tracing::info!("binding on {}:{}", args.host, args.port);
let listener = std::net::TcpListener::bind(format!("{}:{}", args.host, args.port)).unwrap();
loop {
let (stream, addr) = listener.accept().unwrap();
std::thread::spawn(move || {
tracing::info!("got connect from {:?}", addr);
let mut ws = tungstenite::accept(stream).unwrap();
loop {
let msg = ws.read().unwrap();
if msg.is_close() {
break;
}
ws.write(msg).unwrap();
}
tracing::info!("one conn down");
})
.join()
.unwrap();
}
}