#websocket #asynchronous #async #tokio #framework

blunt

Highly opinionated way to build asynchronous Websocket servers with Rust

8 releases

0.0.8 Jul 25, 2021
0.0.7 Jun 23, 2021
0.0.4 Apr 27, 2021

#35 in WebSocket

Download history 2/week @ 2022-03-05 25/week @ 2022-03-12 32/week @ 2022-03-19 1/week @ 2022-03-26 10/week @ 2022-04-23 16/week @ 2022-04-30 48/week @ 2022-05-07 137/week @ 2022-05-14 28/week @ 2022-05-21 60/week @ 2022-05-28 58/week @ 2022-06-04 1/week @ 2022-06-11 3/week @ 2022-06-18

122 downloads per month

MIT OR Apache-2.0 OR MPL-2.0

37KB
779 lines

Blunt

github crates.io docs.rs build status

Highly opinionated way to build asynchronous Websocket servers with Rust

How

The world famous example, echo server

#[tokio::main]
async fn main() -> Result<()> {
    let handler = EchoServer::default();
    ::blunt::builder()
        .for_path("/echo", handler)
        .build()
        .bind("127.0.0.1:3000".parse().expect("Socket Addr"))
        .await?
    
    // now connect your clients to http://127.0.0.1:3000/echo and say something!
}

#[derive(Debug, Default)]
pub struct EchoServer;

#[blunt::async_trait]
impl WebSocketHandler for EchoServer {
    async fn on_open(&mut self, ws: &WebSocketSession) {
        info!("new connection open with id: {}", ws.id());
    }

    async fn on_message(&mut self, ws: &WebSocketSession, msg: WebSocketMessage) {
        ws.send(msg).expect("Unable to send message");
    }

    async fn on_close(&mut self, ws: &WebSocketSession, _msg: WebSocketMessage) {
        info!("connection closed for session id {}", ws.id());
    }
}

For more code examples please see the examples folder.

License

Tri-Licensed under either of Apache License, Version 2.0, MIT license or MPL-2.0 license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be tri-licensed as above, without any additional terms or conditions.

Dependencies

~8–15MB
~281K SLoC