1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! <p align="center">
//! <img src="https://raw.githubusercontent.com/w-henderson/Humphrey/master/assets/logo.png" width=250><br><br>
//! <img src="https://img.shields.io/badge/language-rust-b07858?style=for-the-badge&logo=rust" style="margin-right:5px">
//! <img src="https://img.shields.io/github/workflow/status/w-henderson/Humphrey/CI?style=for-the-badge" style="margin-right:5px">
//! <img src="https://img.shields.io/crates/v/humphrey-ws?style=for-the-badge" style="margin-right:5px">
//! </p>
//!
//! # WebSocket support for Humphrey.
//! 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.
//!
//! ## Features
//! - Performs WebSocket handshake and implements WebSocket according to [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455).
//! - Well-integrated with the core Humphrey crate
//! - Has no dependencies in accordance with Humphrey's goals of being dependency-free. This means SHA-1 ([RFC 3174](https://datatracker.ietf.org/doc/html/rfc3174)) and base64 ([RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648)) are both implemented from scratch in this crate as they are required for the handshake.
//!
//! ## Installation
//! The Humphrey WebSocket crate can be installed by adding `humphrey_ws` to your dependencies in your `Cargo.toml` file.
//!
//! ## Documentation
//! The Humphrey WebSocket documentation can be found at [docs.rs](https://docs.rs/humphrey-ws/).
//!
//! ## Basic Example
//! ```rs
//! use humphrey::App;
//!
//! use humphrey_ws::error::WebsocketError;
//! use humphrey_ws::message::Message;
//! use humphrey_ws::stream::WebsocketStream;
//! use humphrey_ws::websocket_handler;
//!
//! use std::net::TcpStream;
//! use std::sync::Arc;
//!
//! fn main() {
//! let app: App<()> = App::new()
//! .with_websocket_handler(websocket_handler(my_handler));
//! app.run("0.0.0.0:80").unwrap();
//! }
//!
//! fn my_handler(mut stream: WebsocketStream<TcpStream>, _: Arc<()>) {
//! let hello_world = Message::new("Hello, World!");
//! stream.send(hello_world).unwrap();
//!
//! loop {
//! match stream.recv() {
//! Ok(msg) => println!("Message: {}", msg.text().unwrap()),
//! Err(WebsocketError::ConnectionClosed) => {
//! break;
//! },
//! _ => ()
//! }
//! }
//!
//! println!("Connection closed");
//! }
//! ```
//!
//! ## Further Examples
//! - [Echo Server](https://github.com/w-henderson/Humphrey/tree/master/examples/websocket): echoes received messages back to the client with an incrementing number at the end
const MAGIC_STRING: &str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
pub use websocket_handler;