Skip to main content

Module websocket

Module websocket 

Source
Expand description

WebSocket protocol support — RFC 6455.

Provides the building blocks for adding WebSocket endpoints to a server: upgrade detection, the opening handshake, and frame-level read/write.

§Integration pattern

WebSocket connections require taking over the raw TCP stream after sending the 101 response, so they cannot be handled inside a normal Controller::process call (which has no access to the stream). The recommended pattern is to bypass Server::run for upgraded connections and drive them with your own accept loop:

use std::net::TcpListener;
use rust_web_server::websocket::{WebSocket, Frame};
use rust_web_server::request::Request;
use rust_web_server::response::Response;

let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
for stream in listener.incoming() {
    let mut stream = stream.unwrap();
    // peek / read request bytes, decide if it's a WS upgrade
    // (simplified — real code should read the full request first)
    let raw = vec![0u8; 4096];
    // ... parse into Request, then:
    //
    // if WebSocket::is_upgrade_request(&request) {
    //     let response = WebSocket::handshake_response(&request).unwrap();
    //     // write the 101 response to stream, then frame loop
    //     loop {
    //         match WebSocket::read_frame(&mut stream) { ... }
    //     }
    // } else {
    //     // normal Server::process
    // }
}

Structs§

WebSocket
WebSocket protocol utilities.

Enums§

Frame
A decoded WebSocket frame.