Expand description
WebSocket protocol implementation (RFC 6455).
This module provides a complete WebSocket implementation built on asupersync’s I/O primitives, with no external dependencies for SHA-1 or base64.
§Architecture
The implementation is layered:
- Handshake — HTTP upgrade negotiation (101 Switching Protocols)
- Frame codec — Binary frame parsing/encoding per RFC 6455 §5
- WebSocket — High-level API matching FastAPI/Starlette semantics
§Example
ⓘ
use fastapi_http::websocket::{WebSocket, Message};
async fn handler(mut ws: WebSocket) {
ws.accept(None).await.unwrap();
loop {
match ws.receive().await {
Ok(Message::Text(text)) => {
ws.send_text(&text).await.unwrap();
}
Ok(Message::Close(_, _)) | Err(_) => break,
Ok(Message::Binary(_)) => {}
Ok(Message::Ping(_) | Message::Pong(_)) => unreachable!(),
}
}
}Structs§
- WebSocket
- A WebSocket connection.
- WebSocket
Config - Configuration for WebSocket connections.
Enums§
- Close
Code - WebSocket close status code (RFC 6455 §7.4.1).
- Message
- A WebSocket message (assembled from one or more frames).
- Opcode
- WebSocket frame opcode (RFC 6455 §5.2).
- WebSocket
Error - WebSocket error type.
Constants§
- DEFAULT_
MAX_ FRAME_ SIZE - Default maximum frame payload size (16 MiB).
- DEFAULT_
MAX_ MESSAGE_ SIZE - Default maximum message size (64 MiB, for multi-frame messages).
Functions§
- accept_
key - Compute the
Sec-WebSocket-Acceptvalue from the client’sSec-WebSocket-Keyheader (RFC 6455 §4.2.2 step 4). - build_
accept_ response - Build the HTTP 101 Switching Protocols response bytes for a WebSocket upgrade.
- validate_
upgrade_ request - Validate that an HTTP request is a valid WebSocket upgrade request.