Struct std::WsConn
struct WsConn {
int fd;
?&heap TlsStream tls;
int is_server;
?&heap unsigned char early;
unsigned long earlyLen;
unsigned long earlyPos;
}Fields
the TLS session of a wss:// connection, else null
1 = accepted server-side (frames we send are unmasked, frames we receive must be masked); 0 = client-side (the reverse)
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.
Methods
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 · definitionConnects 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 · definitionconnect 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 · definitionconnect_timeout over TLS when 'tls' is set (wss://; 'cfg' null: TlsConfig::new()'s defaults — system roots, host name verified).
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)) { ... }
Sends a close frame. Does not close the underlying socket — call close() (below) to do both.
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 · definitionstatic struct WsConn accept_upgrade(const &HttpRequest req, &HttpConn conn, ?&int ok)source · definitionCompletes 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.
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).
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.
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).
= drop()