http-ws

websocket for http crate type

3 releases (breaking)

0.3.0 Mar 4, 2024
0.2.0 Jan 19, 2024
0.1.0 Dec 29, 2023

#211 in WebSocket

Download history 49/week @ 2024-06-28 13/week @ 2024-07-05 37/week @ 2024-07-12 154/week @ 2024-07-19 53/week @ 2024-07-26 65/week @ 2024-08-02 105/week @ 2024-08-09 148/week @ 2024-08-16 220/week @ 2024-08-23 232/week @ 2024-08-30 318/week @ 2024-09-06 343/week @ 2024-09-13 499/week @ 2024-09-20 394/week @ 2024-09-27 126/week @ 2024-10-04 31/week @ 2024-10-11

1,083 downloads per month
Used in xitca-web

Apache-2.0

62KB
1K SLoC

an async websocket crate.

Features

  • common http types and streaming interface for easy integration.

Requirement

  • Rust 1.75
  • http and futures for http types and async streaming interaction[^1]

[^1]: see project Cargo.toml for dependency versioning.


lib.rs:

WebSocket protocol using high level API that operate over futures_core::Stream trait.

HTTP type

  • http crate types are used for input and output
  • support http/1.1 and http/2

Examples

use http::{header, Request, StatusCode};
use http_ws::handshake;

// an incoming http request.
let request = Request::get("/")
    .header(header::UPGRADE, header::HeaderValue::from_static("websocket"))
    .header(header::CONNECTION, header::HeaderValue::from_static("upgrade"))
    .header(header::SEC_WEBSOCKET_VERSION, header::HeaderValue::from_static("13"))
    .header(header::SEC_WEBSOCKET_KEY, header::HeaderValue::from_static("some_key"))
    .body(())
    .unwrap();

let method = request.method();
let headers = request.headers();

// handshake with request and return a response builder on success.
let response_builder = handshake(method, headers).unwrap();

// add body to builder and finalized it.
let response = response_builder.body(()).unwrap();

// response is valid response to websocket request.
assert_eq!(response.status(), StatusCode::SWITCHING_PROTOCOLS);

async HTTP body

Please reference [ws] function

Dependencies

~1.6–7.5MB
~53K SLoC