#websocket-server #socket-server #frame

websocket-server-async

rust websocket server tokio frame

5 unstable releases

0.3.0 Jan 18, 2024
0.2.2 Sep 19, 2022
0.1.6 Apr 27, 2022
0.1.3 Mar 20, 2022
0.1.0 Feb 4, 2022

#192 in WebSocket

41 downloads per month

MIT/Apache

20KB
448 lines

#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(())
}


Dependencies

~11–22MB
~407K SLoC