Expand description
Make a WebSocket carry a raw byte stream, and serve/proxy HTTP/2 over it.
accept performs the server-side WebSocket handshake on a hyper request
(pluggable into any hyper/axum route) and yields the upgraded
connection as a byte stream. bridge then pumps bytes full-duplex between
that stream and any AsyncRead + AsyncWrite peer (a TCP upstream, an
in-process h2c server, …). WebSocket message payloads become a continuous
byte stream, so h2c framing rides straight through.
Framing is done by wslay (vendored
C, via the wslay-sys crate). Driven through its event API with buffering
off, wslay streams each frame’s payload incrementally — it never holds a
whole frame in memory, no matter how large — and auto-handles ping/close.
Three entry points sit on top of bridge:
WsByteStream— the WebSocket as anAsyncRead + AsyncWritehandle.serve_h2— run any hyperServiceas HTTP/2 over the tunnel.- the
h2ts-proxybinary — a standalone WS→upstream-h2c proxy.
Part of h2ts: this server pairs with
the TypeScript @debdattabasu/h2ts
and Rust/WASM h2ts-client frontend
clients — both behavior-mirrored against it by the shared conformance suite —
or any client that forwards a byte stream over a WebSocket.
Structs§
- Accept
Options - Options controlling how the handshake picks a subprotocol to echo when the
handler’s selector declines (see
accept_with_options). - Bridge
Config - Control-frame configuration and hooks for
bridge_with/serve_h2_with. - Close
Frame - A WebSocket close: status code and (UTF-8) reason. Uses RFC 6455 close codes; the reason should be at most 123 bytes.
- Control
Receiver - The receiving half of a
control_channel; give it toBridgeConfig::control. - Keep
Alive - Server-initiated keepalive: send a Ping when the connection goes idle, and disconnect the peer if it doesn’t respond in time.
- Serve
Config - Configuration for
serve_h2_with_config: the WebSocketBridgeConfigplus an optional HTTP/2 idle timeout. - WsByte
Stream - A WebSocket presented as a raw byte duplex (
AsyncRead + AsyncWrite) — item 1 in its “looks like a TCP stream” form. - WsControl
- Sends WebSocket control frames into a running
bridge_with. Cheaply cloneable; call from any task. Frames are queued and flushed by the bridge on its next turn; a send fails only once the bridge has ended.
Enums§
- WebSocket
Error - Errors from the WebSocket handshake.
Constants§
- DEFAULT_
SUBPROTOCOL - The subprotocol h2ts clients offer by default.
acceptechoes it when the client offered it and the handler didn’t choose another (seeaccept_with).
Functions§
- accept
- Accept a WebSocket upgrade, requiring the
DEFAULT_SUBPROTOCOL(h2ts). - accept_
with - Accept a WebSocket upgrade, choosing which offered subprotocol to echo.
- accept_
with_ options - Accept a WebSocket upgrade, choosing which offered subprotocol to echo, with
explicit
AcceptOptions. - bridge
- Pump bytes full-duplex between a WebSocket and a byte-stream peer until either
side closes, using wslay for framing. Equivalent to
bridge_withwith a defaultBridgeConfig. - bridge_
with - Like
bridge, but with control-frame configuration and hooks (BridgeConfig): send control frames viacontrol_channel, observe received close/ping/pong, and set the close sent on teardown. - control_
channel - Create a control channel. Keep the
WsControlto send control frames; put theControlReceiverinBridgeConfig::control. - is_
upgrade_ request - Whether
requestis a WebSocket upgrade:Upgrade: websocket,Connection: upgrade, and aSec-WebSocket-Key. - offered_
protocols - The WebSocket subprotocols the client offered, in order, whitespace-trimmed;
empty if it offered none. Gives a handler full visibility into the offer so
it can pass one to
accept_with. - serve_
h2 - Serve any hyper HTTP/2 service over a WebSocket tunnel (item 2).
- serve_
h2_ with - Like
serve_h2, but with control-frame configuration and hooks (BridgeConfig) applied to the underlying WebSocket bridge — send control frames via acontrol_channel, observe the peer’s close reason, etc. - serve_
h2_ with_ config - Like
serve_h2_with, but also accepts an HTTP/2idle_timeoutviaServeConfig: after that long with no open HTTP/2 streams, the connection is closed with a gracefulGOAWAY(then the WebSocket close), so a healthy but idle client reconnects fresh. hyper has no built-in idle timeout, so this tracks open streams and drivesgraceful_shutdownitself; a live stream (even a quiet one) is never reaped, and pings never reset the timer.
Type Aliases§
- Close
Hook - Hook invoked with the close frame describing why the connection ended.
- Control
Hook - Hook invoked with a received Ping/Pong payload.
- Upgraded
Io - The upgraded WebSocket connection, presented as a raw tokio byte stream
(
AsyncRead + AsyncWrite). Hand it tobridge,serve_h2, orWsByteStream.