Skip to main content

Module ws_proxy

Module ws_proxy 

Source
Expand description

WebSocket reverse proxy.

WsProxy listens for incoming TCP connections, reads the initial HTTP request, verifies it is a WebSocket upgrade, connects to a backend, performs the WebSocket handshake end-to-end, and then bidirectionally tunnels raw WebSocket bytes between the client and the backend.

Plain (ws://) backends use two threads (one per direction) via std::io::copy, identical to the original implementation.

TLS (wss://) backends use a single-thread polling loop: both streams are set to a 5 ms read timeout and the loop alternates between the two directions, sleeping 1 ms when neither side has data. This avoids the deadlock that arises when trying to share a rustls::StreamOwned between two blocking threads.

§Example

use rust_web_server::ws_proxy::WsProxy;

// Plain WebSocket — two backends, round-robin.
WsProxy::new(["ws://chat-backend:9000", "ws://chat-backend:9001"])
    .connect_timeout_ms(3000)
    .bind("0.0.0.0:8080")
    .unwrap();

// TLS WebSocket (requires http-client or http2 feature).
WsProxy::new(["wss://chat-backend.internal:443"])
    .bind("0.0.0.0:8080")
    .unwrap();

Structs§

WsProxy
WebSocket reverse proxy with round-robin load balancing.