Crates.io | blunt |
lib.rs | blunt |
version | 0.0.8 |
source | src |
created_at | 2021-04-25 20:04:17.659542 |
updated_at | 2021-07-25 23:26:36.94744 |
description | Highly opinionated way to build asynchronous Websocket servers with Rust. |
homepage | https://github.com/njasm/blunt/ |
repository | https://github.com/njasm/blunt/ |
max_upload_size | |
id | 389414 |
size | 121,127 |
Highly opinionated way to build asynchronous Websocket servers with Rust
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.
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.