2 个版本
0.1.1 | 2024 年 4 月 19 日 |
---|---|
0.1.0 | 2024 年 4 月 15 日 |
0.0.0 |
|
#153 in WebSocket
12KB
226 行
Pws
为 tokio-tungstenite 提供持久化 WebSocket 连接。
示例
在 示例 文件夹中查看更多示例。
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");
}
依赖项
~5–16MB
~234K SLoC