websocket-server-async

Crates.iowebsocket-server-async
lib.rswebsocket-server-async
version0.3.0
sourcesrc
created_at2022-02-04 12:31:52.66995
updated_at2024-01-18 08:17:04.166837
descriptionrust websocket server tokio frame.
homepage
repositoryhttps://github.com/luyikk/websocket-server-async
max_upload_size
id526762
size51,886
(luyikk)

documentation

https://docs.rs/websocket-server-async

README

#rust websocket tokio server frame.

Examples Echo

use anyhow::Result;
use futures_util::StreamExt;
use log::*;
use websocket_server_async::{Builder, IPeer, IWebSocketServer};

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::Builder::new()
        .filter_level(log::LevelFilter::Debug)
        .init();
    let websocket_server = Builder::new("0.0.0.0:8888")
        .set_connect_event(|addr| {
            info!("{} connect", addr);
            true
        })
        .set_input_event(|mut reader, peer, _| async move {
            while let Some(msg) = reader.next().await {
                let msg = msg?;
                if msg.is_text() || msg.is_binary() {
                    peer.send_message(msg).await?;
                }
            }
            Ok(())
        })
        .build()
        .await;
    websocket_server.start_block(()).await?;
    Ok(())
}


Commit count: 17

cargo fmt