std1.0.0-beta-7 SafeC docs

std :: http::websocket

Struct std::WsConn

#include <std/http/websocket.h> source

struct WsConn {
    int fd;
    ?&heap TlsStream tls;
    int is_server;
    ?&heap unsigned char early;
    unsigned long earlyLen;
    unsigned long earlyPos;
}

Fields

§int fdsource

the TLS session of a wss:// connection, else null

§int is_serversource

1 = accepted server-side (frames we send are unmasked, frames we receive must be masked); 0 = client-side (the reverse)

§?&heap unsigned char earlysource

Bytes the peer sent right behind its handshake (in the same read as the handshake's last line): the start of its first frame, which recv() consumes before reading the socket. Heap-owned; null when nothing came early. close() frees it.

§unsigned long earlyLensource
§unsigned long earlyPossource

Methods

§static struct WsConn closed()source · definition

A closed connection (fd = -1, nothing owned): the value to start from before accept(), or a placeholder. Dropping or closing it does nothing.

§static struct WsConn connect( const &char host, unsigned short port, const &char path, ?&int ok, )source · definition

Connects to ws://host:port/path — performs the TCP connect, sends the HTTP Upgrade request with a fresh random Sec-WebSocket-Key, and verifies the server's Sec-WebSocket-Accept matches the RFC 6455 formula before returning. On any failure (connect, non-101 response, key mismatch), '*ok' is set to 0.

§static struct WsConn connect_timeout( const &char host, unsigned short port, const &char path, int timeout_ms, ?&int ok, )source · definition

connect with an explicit limit: 'timeout_ms' bounds the TCP connect and each send/receive of the opening handshake (a server that accepts but never answers the Upgrade fails after that long). It is cleared once the handshake succeeds, so recv() on the returned connection still waits indefinitely for the next message. <= 0 means no limit. connect uses WS_DEFAULT_TIMEOUT_MS. DNS resolution is not covered (see http_request_timeout).

§static struct WsConn connect_tls( const &char host, unsigned short port, const &char path, int tls, const ?&TlsConfig cfg, int timeout_ms, ?&int ok, )source · definition

connect_timeout over TLS when 'tls' is set (wss://; 'cfg' null: TlsConfig::new()'s defaults — system roots, host name verified).

§int accept(int listenFd)source · definition

Blocks until one client connects to 'listenFd' (from ws_listen) and completes the WebSocket handshake (reads its HTTP Upgrade request, validates it, sends back the 101 response with the computed Sec-WebSocket-Accept). On success, fills this connection in place and returns 1; on a connection that fails to complete the handshake, returns 0 (the underlying fd is already closed) and leaves this connection closed (fd = -1). Whatever this WsConn held before is overwritten, not closed: accept into a WsConn::closed() (or a connection already closed). struct WsConn c = std::WsConn::closed(); defer c.drop(); if (c.accept(lfd)) { ... }

§int send_text(const &char text)source · definition
§int send_binary([]const unsigned char data)source · definition
§int send_ping()source · definition
§int send_pong()source · definition
§int send_close()source · definition

Sends a close frame. Does not close the underlying socket — call close() (below) to do both.

§static struct WsConn connect_url(const &char url, int timeout_ms, ?&int ok)source · definition

Connects to a ws:// or wss:// URL ("wss://host:port/path?query"; wss defaults to port 443 and TLS with TlsConfig::new()'s defaults), with connect_timeout's limit. connect_url_tls takes the TLS settings.

§static struct WsConn connect_url_tls( const &char url, const ?&TlsConfig cfg, int timeout_ms, ?&int ok, )source · definition
§static struct WsConn accept_upgrade(const &HttpRequest req, &HttpConn conn, ?&int ok)source · definition

Completes the WebSocket handshake for a request an HttpServer/Router handler received (check ws_is_upgrade(req) first): writes the 101 response, takes the socket over (conn.detach — the server then leaves the connection alone) and returns the WsConn. *ok = 0 if the request isn't a valid version-13 upgrade (the handler should then answer it normally, e.g. 400/426). The handler may serve the WebSocket right there (blocking that worker) or hand it to a thread.

§int recv_message(&WsMessage out, unsigned long max_len)source · definition

Reads one whole message: continuation frames are reassembled, pings are answered with a pong automatically and pongs skipped, so 'out' is a WS_OPCODE_TEXT/BINARY message or a WS_OPCODE_CLOSE frame. 0 on a connection error, a protocol error, or a message over 'max_len' bytes (0 = no limit).

§int recv(&WsMessage out)source · definition

Reads one complete frame (see the file-level comment on fragmentation; recv_message reassembles). Returns 1 with 'out' filled on success, 0 on a connection error/EOF/malformed frame. A WS_OPCODE_CLOSE message is returned like any other (not treated specially) — check for it and call close() yourself.

§void drop()source · definition

Sends a close frame (best-effort — ignores failure, since the peer may already be gone) and closes the socket. Nothing is sent or closed for a closed connection (fd < 0: WsConn::closed(), a failed connect or accept, an earlier close).

§void close() consumesource · definition

= drop()