Skip to content
CryoCryo home
Stdlibnet::ws

conn

import std::net::ws::conn; · source

Types

WebSocket<S>

type struct WebSocket<S> {
    conn:      BufStream<S>;
    is_client: boolean;
    closed:    boolean;

    static of(conn: BufStream<S>, is_client: boolean) -> WebSocket<S>;
    connection(mut &this) -> BufStream<S>*;
    is_closed(&this) -> boolean;

    async client_handshake(mut &this, host: Str, path: Str) -> Result<(), IoError>
    where S: AsyncTransport;

    async server_handshake(mut &this) -> Result<(), IoError>
    where S: AsyncTransport;

    async send_text(mut &this, text: Str) -> Result<(), IoError>
    where S: AsyncTransport;

    async send_binary(mut &this, data: Slice<u8>) -> Result<(), IoError>
    where S: AsyncTransport;

    async ping(mut &this, data: Slice<u8>) -> Result<(), IoError>
    where S: AsyncTransport;

    async close(mut &this, code: u16, reason: Str) -> Result<(), IoError>
    where S: AsyncTransport;

    async recv(mut &this) -> Result<Message, IoError>
    where S: AsyncTransport;
}

A WebSocket<S> owns a BufStream<S> over any AsyncTransport. connect(sock, host, path) and accept(sock) wrap a plain TcpStream and run the handshake in one step. For a TLS transport, or to control the buffering, build with of(conn, is_client) and call client_handshake(host, path) or server_handshake() yourself. The handshake is an HTTP/1.1 Upgrade.

mut ws: WebSocket<TlsStream> = WebSocket::<TlsStream>::of(BufStream::<TlsStream>::of(tls), true);
await ws.client_handshake(host, path)?;

The message surface is send_text, send_binary, ping, close(code, reason), and recv — all async. Framing covers text and binary messages, fragmentation reassembly, and the Ping/Pong/Close control frames. recv yields application messages only: it answers a Ping with a Pong itself, skips incoming Pongs, and surfaces a Close frame as Message::Close. A bounded number of control frames is serviced per recv, so a peer flooding pings cannot keep the call from ever returning. is_closed() reports whether a close has been sent or received; connection() surfaces the transport for socket options, not for I/O.

Clients mask every frame they send and servers must not, as the RFC requires; is_client is what decides, and a frame arriving with the wrong masking is a protocol error.

Properties

PropertyType
connBufStream<S>
is_clientboolean
closedboolean

Constructors

static of(conn: BufStream<S>, is_client: boolean) -> WebSocket<S>;

Take ownership of an already-upgraded connection. is_client selects masked (client) vs unmasked (server) framing per RFC 6455. Use client_handshake / server_handshake to run the upgrade first.

Methods

connection(mut &this) -> BufStream<S>*;

The buffered connection, for callers that need the transport itself (socket options, the negotiated ALPN protocol). Not for reading or writing — that would desynchronize the framing.

is_closed(&this) -> boolean;

Has a Close frame been sent or received on this connection?

async client_handshake(mut &this, host: Str, path: Str) -> Result<(), IoError> where S: AsyncTransport;

Client handshake: send the GET upgrade, then verify the 101 and the Sec-WebSocket-Accept echo. Bytes that arrived after the blank line stay buffered for recv.

async server_handshake(mut &this) -> Result<(), IoError> where S: AsyncTransport;

Server handshake: read the client GET, reply 101 with the computed accept value. Bytes that arrived after the blank line — a client that pipelined its first frame into the same write — stay buffered.

async send_text(mut &this, text: Str) -> Result<(), IoError> where S: AsyncTransport;

Send a text message in a single frame.

async send_binary(mut &this, data: Slice<u8>) -> Result<(), IoError> where S: AsyncTransport;

Send a binary message in a single frame.

async ping(mut &this, data: Slice<u8>) -> Result<(), IoError> where S: AsyncTransport;

Send a Ping with optional application data.

async close(mut &this, code: u16, reason: Str) -> Result<(), IoError> where S: AsyncTransport;

Send a Close frame with a status code and reason. The transport itself is closed when the WebSocket is dropped.

async recv(mut &this) -> Result<Message, IoError> where S: AsyncTransport;

Receive the next application message, reassembling fragments and transparently answering Pings with Pongs and skipping Pongs. A Close frame surfaces as Message::Close.

Message

type enum Message {
    Text(String);
    Binary(Array<u8>);
    Close;
}

What recv yields: a reassembled text or binary message, or the peer's close.

Variants
Variant
Text(String)
Binary(Array<u8>)
Close

Trait implementations

implement<S> trait Drop for struct WebSocket<S>

Dropping the socket drops the connection. Send close first if you want the peer to see a clean shutdown rather than a reset.

Free Functions

async function connect(sock: TcpStream, host: Str, path: Str) -> Result<WebSocket<TcpStream>, IoError>;

Connect a WebSocket over an already-connected TCP socket: wrap it, run the client handshake, and hand back the upgraded connection. The socket is consumed — the WebSocket owns it from here.

The one-step client constructor over a plain TcpStream: runs the opening handshake against host and path and returns a ready WebSocket<TcpStream>.

async function accept(sock: TcpStream) -> Result<WebSocket<TcpStream>, IoError>;

Accept a WebSocket over an accepted TCP socket: wrap it, run the server handshake, and hand back the upgraded connection.

The server-side counterpart: answers a client's upgrade request on an accepted TcpStream and returns the ready WebSocket<TcpStream>.

Constants

const MAX_HEAD_LINE:        u64 = 8192;
const MAX_HEAD_LINES:       u64 = 100;
const MAX_MESSAGE:          u64 = 67108864;
const MAX_CONTROL_PER_RECV: u64 = 256;