Crate http_ws

Source
Expand description

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

Re-exports§

pub use self::stream::RequestStream;
pub use self::stream::ResponseSender;
pub use self::stream::ResponseStream;
pub use self::stream::ResponseWeakSender;
pub use self::stream::WsError;

Modules§

stream

Structs§

CloseReason
Reason for closing the connection
Codec
WebSocket protocol codec.

Enums§

CloseCode
Status code used to indicate why an endpoint is closing the WebSocket connection.
HandshakeError
WebSocket handshake errors
Item
A WebSocket continuation item.
Message
A WebSocket message.
OpCode
Operation codes as part of RFC6455.
ProtocolError
WebSocket protocol errors.

Functions§

client_request_from_uri
Prepare a Request with given Uri and Version for websocket connection. Only Version::HTTP_11 and Version::HTTP_2 are supported. After process the request would be ready to be sent to server.
handshake
Verify HTTP/1.1 WebSocket handshake request and create handshake response.
handshake_h2
Verify HTTP/2 WebSocket handshake request and create handshake response.
hash_key
Hashes the Sec-WebSocket-Key header according to the WebSocket spec.
ws
A shortcut for generating a set of response types with given Request and <Body> type.

Type Aliases§

WsOutput