Crates.io | pws |
lib.rs | pws |
version | 0.1.1 |
source | src |
created_at | 2024-04-13 03:55:11.209763 |
updated_at | 2024-04-19 18:00:23.822831 |
description | Persistent Websocket connection for tokio-tungstenite |
homepage | https://github.com/rellfy/pws |
repository | https://github.com/rellfy/pws |
max_upload_size | |
id | 1207357 |
size | 43,204 |
Persistent Websocket connection for tokio-tungstenite.
See more examples in the examples folder.
use pws::{connect_persistent_websocket_async, Message, Url};
use std::str::FromStr;
#[tokio::main]
async fn main() {
let url = Url::from_str("wss://echo.websocket.org").unwrap();
let (tx, mut rx) = connect_persistent_websocket_async(url).await.unwrap();
let mut close_count = 0;
while let Ok(msg) = rx.recv().await {
match msg {
Message::Text(msg) => {
println!("received: {msg}");
// Simulate a connection close.
tx.send(Message::Close(None)).await.unwrap();
}
Message::ConnectionOpened => {
println!("connection opened ({close_count})");
let msg = format!("hello! connection #{close_count}");
tx.send(Message::Text(msg)).await.unwrap();
}
Message::ConnectionClosed => {
println!("connection closed");
close_count += 1;
if close_count >= 2 {
// Terminate the persistent connection.
break;
}
}
_ => {}
}
}
println!("persistent websocket is done");
}