Highly opinionated way to build asynchronous Websocket servers with Rust
The world famous example, echo server
#[tokio::main]asyncfnmain() -> Result<()>{::blunt::builder().for_path_with_ctor("/echo", |ctx| EchoServer{ ctx }).build().bind("127.0.0.1:3000".parse().expect("Invalid Socket Addr")).await?;Ok(())// now connect your clients to http://127.0.0.1:3000/echo and say something!}#[derive(Debug,Default)]pubstructEchoServer{ctx:AppContext,}#[blunt::async_trait]implWebSocketHandlerforEchoServer{asyncfnon_open(&mutself,session_id:Uuid){self.ctx.session(session_id).await.and_then(|s| {
s.send(WebSocketMessage::Text(String::from("Welcome to Echo server!",))).ok()});}asyncfnon_message(&mutself,session_id:Uuid,msg:WebSocketMessage){self.ctx.session(session_id).await.and_then(|s| s.send(msg).ok());}asyncfnon_close(&mutself,session_id:Uuid,_msg:WebSocketMessage){info!("connection closed for session id {}", session_id);}}For more code examples please see the examples folder.
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.