pub struct WebSocket;Expand description
WebSocket protocol utilities.
Implementations§
Source§impl WebSocket
impl WebSocket
Sourcepub const MAX_FRAME_PAYLOAD: u64
pub const MAX_FRAME_PAYLOAD: u64
The largest frame payload WebSocket::read_frame accepts (16 MiB);
a frame claiming more is an error. Use
WebSocket::read_frame_limited for another limit.
Sourcepub fn is_upgrade_request(request: &Request) -> bool
pub fn is_upgrade_request(request: &Request) -> bool
Returns true if request is a valid WebSocket upgrade request
(has Upgrade: websocket, Connection: upgrade, and Sec-WebSocket-Key).
Sourcepub fn handshake_response(request: &Request) -> Result<Response, String>
pub fn handshake_response(request: &Request) -> Result<Response, String>
Build the HTTP 101 Switching Protocols response for a WebSocket
opening handshake. Returns an error if Sec-WebSocket-Key is absent.
Write the raw bytes from Response::generate_response to the stream,
then transition to frame-level I/O with WebSocket::read_frame /
WebSocket::write_frame.
Sourcepub fn accept_key(client_key: &str) -> String
pub fn accept_key(client_key: &str) -> String
Compute the Sec-WebSocket-Accept value from the client’s
Sec-WebSocket-Key using SHA-1 and base64 as specified in RFC 6455.
Sourcepub fn read_frame(stream: &mut impl Read) -> Result<Frame, String>
pub fn read_frame(stream: &mut impl Read) -> Result<Frame, String>
Read one WebSocket frame from stream.
Handles client-to-server masking automatically. Returns an error if the stream closes unexpectedly or contains a protocol violation.
A frame whose declared payload exceeds WebSocket::MAX_FRAME_PAYLOAD
is refused before any of it is read: the length comes from the peer,
and allocating it up front (up to 2^64 − 1 bytes) aborted the process.
Sourcepub fn read_frame_limited(
stream: &mut impl Read,
max_payload: u64,
) -> Result<Frame, String>
pub fn read_frame_limited( stream: &mut impl Read, max_payload: u64, ) -> Result<Frame, String>
WebSocket::read_frame with max_payload bytes as the limit. The
payload is read as it arrives, so memory follows what the peer
actually sends, not what it claims.
Sourcepub fn write_frame(stream: &mut impl Write, frame: Frame) -> Result<(), String>
pub fn write_frame(stream: &mut impl Write, frame: Frame) -> Result<(), String>
Write a WebSocket frame to stream (server→client, unmasked).
Sourcepub fn send_text(stream: &mut impl Write, text: &str) -> Result<(), String>
pub fn send_text(stream: &mut impl Write, text: &str) -> Result<(), String>
Convenience: send a text message.