https://github.com/njasm/blunt
Highly opinionated way to build asynchronous Websocket servers with Rust
https://github.com/njasm/blunt
asynchronous network-programming opinionated rust rust-lang tokio websocket websockets
Last synced: 28 days ago
JSON representation
Highly opinionated way to build asynchronous Websocket servers with Rust
- Host: GitHub
- URL: https://github.com/njasm/blunt
- Owner: njasm
- License: apache-2.0
- Created: 2021-04-25T15:23:23.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-10-01T23:32:50.000Z (over 3 years ago)
- Last Synced: 2025-02-13T05:15:51.711Z (3 months ago)
- Topics: asynchronous, network-programming, opinionated, rust, rust-lang, tokio, websocket, websockets
- Language: Rust
- Homepage:
- Size: 178 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE-2.0
Awesome Lists containing this project
README
Blunt
=[
](https://github.com/njasm/blunt)
[](https://crates.io/crates/blunt)
[](https://docs.rs/blunt)
[](https://github.com/njasm/blunt/actions?query=branch%3Amaster)
Highly opinionated way to build asynchronous Websocket servers with Rust
### How
The world famous example, echo server
```rust
#[tokio::main]
async fn main() -> 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)]
pub struct EchoServer {
ctx: AppContext,
}#[blunt::async_trait]
impl WebSocketHandler for EchoServer {
async fn on_open(&mut self, session_id: Uuid) {
self.ctx
.session(session_id)
.await
.and_then(|s| {
s.send(WebSocketMessage::Text(String::from(
"Welcome to Echo server!",
))).ok()
});
}async fn on_message(&mut self, session_id: Uuid, msg: WebSocketMessage) {
self.ctx
.session(session_id)
.await
.and_then(|s| s.send(msg).ok());
}async fn on_close(&mut self, session_id: Uuid, _msg: WebSocketMessage) {
info!("connection closed for session id {}", session_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.