pws

Crates.iopws
lib.rspws
version0.1.1
sourcesrc
created_at2024-04-13 03:55:11.209763
updated_at2024-04-19 18:00:23.822831
descriptionPersistent Websocket connection for tokio-tungstenite
homepagehttps://github.com/rellfy/pws
repositoryhttps://github.com/rellfy/pws
max_upload_size
id1207357
size43,204
Lorenzo Fontoura (rellfy)

documentation

README

Pws

Persistent Websocket connection for tokio-tungstenite.

Example

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");
}
Commit count: 21

cargo fmt