The core Humphrey crate does not support WebSocket connections, but through its WebsocketHandler
trait, it can be extended to support them, which is what this crate does.
The Humphrey WebSocket crate can be installed by adding humphrey_ws
to your dependencies in your Cargo.toml
file.
The Humphrey WebSocket documentation can be found at docs.rs.
```rs use humphrey::App;
use humphreyws::error::WebsocketError; use humphreyws::message::Message; use humphreyws::stream::WebsocketStream; use humphreyws::websocket_handler;
use std::net::TcpStream; use std::sync::Arc;
fn main() { let app: App<()> = App::new() .withwebsockethandler(websockethandler(myhandler)); app.run("0.0.0.0:80").unwrap(); }
fn myhandler(mut stream: WebsocketStream
loop {
match stream.recv() {
Ok(msg) => println!("Message: {}", msg.text().unwrap()),
Err(WebsocketError::ConnectionClosed) => {
break;
},
_ => ()
}
}
println!("Connection closed");
} ```