/
main.rs
48 lines (41 loc) 路 1.16 KB
/
main.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
46
47
48
use async_trait::async_trait;
use ezsockets::BoxError;
use ezsockets::ClientConfig;
use std::io::BufRead;
use url::Url;
struct Client {}
#[async_trait]
impl ezsockets::ClientExt for Client {
type Message = ();
async fn text(&mut self, text: String) -> Result<(), BoxError> {
tracing::info!("received message: {text}");
Ok(())
}
async fn binary(&mut self, bytes: Vec<u8>) -> Result<(), BoxError> {
tracing::info!("received bytes: {bytes:?}");
Ok(())
}
async fn call(&mut self, message: Self::Message) {
match message {
() => {}
}
}
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let url = format!("ws://127.0.0.1:8080");
let url = Url::parse(&url).unwrap();
let config = ClientConfig::new(url);
let (handle, future) = ezsockets::connect(|_| Client {}, config).await;
tokio::spawn(async move {
future.await.unwrap();
});
let stdin = std::io::stdin();
let lines = stdin.lock().lines();
for line in lines {
let line = line.unwrap();
tracing::info!("sending {line}");
handle.text(line).await;
}
}