-
Notifications
You must be signed in to change notification settings - Fork 10
/
hello_world.rs
36 lines (32 loc) · 944 Bytes
/
hello_world.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
use futures_util::{SinkExt, StreamExt, TryStreamExt};
use reqwest::Client;
use reqwest_websocket::{Error, Message, RequestBuilderExt};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
let websocket = Client::default()
.get("wss://echo.websocket.org/")
.upgrade()
.send()
.await?
.into_websocket()
.await?;
let (mut tx, mut rx) = websocket.split();
futures_util::future::join(
async move {
for i in 1..11 {
tx.send(Message::Text(format!("Hello, World! #{i}")))
.await
.unwrap();
}
},
async move {
while let Some(message) = rx.try_next().await.unwrap() {
if let Message::Text(text) = message {
println!("received: {text}");
}
}
},
)
.await;
Ok(())
}