Skip to main content

Module websocket

Module websocket 

Source
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:

  1. Handshake — HTTP upgrade negotiation (101 Switching Protocols)
  2. Frame codec — Binary frame parsing/encoding per RFC 6455 §5
  3. 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.
WebSocketConfig
Configuration for WebSocket connections.

Enums§

CloseCode
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).
WebSocketError
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-Accept value from the client’s Sec-WebSocket-Key header (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.