8 releases
0.0.8 | Jul 25, 2021 |
---|---|
0.0.7 | Jun 23, 2021 |
0.0.4 | Apr 27, 2021 |
#268 in WebSocket
38KB
779 lines
Blunt
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–18MB
~237K SLoC