blunt

Crates.ioblunt
lib.rsblunt
version0.0.8
sourcesrc
created_at2021-04-25 20:04:17.659542
updated_at2021-07-25 23:26:36.94744
descriptionHighly opinionated way to build asynchronous Websocket servers with Rust.
homepagehttps://github.com/njasm/blunt/
repositoryhttps://github.com/njasm/blunt/
max_upload_size
id389414
size121,127
Nelson J Morais (njasm)

documentation

README

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.

Commit count: 94

cargo fmt