abla/websocket

Generated from source.

Import with import "abla/websocket".

Source: abla/websocket/entry.ab

WebSocketFrame (class)

class WebSocketFrame(
    val final: bool,
    val opcode: int,
    val payload: string,
    val masked: bool
)

Holds a frame's final flag, opcode, payload bytes, and masking state.

WebSocketFrameDecode (class)

class WebSocketFrameDecode(
    val frame: WebSocketFrame,
    val consumed: int,
    val complete: bool,
    val error: string
)

Holds the decoded frame, consumed bytes, completion flag, and any frame error.

WebSocketFrameDecode.succeeded (method)

fun succeeded(): bool

Returns true when a complete frame decoded without error.

WebSocketReadResult (class)

class WebSocketReadResult(
    val frame: WebSocketFrame,
    val available: bool,
    val closed: bool,
    val timedOut: bool,
    val error: string
)

Holds a frame or a stream outcome indicating closure, timeout, or error.

WebSocketMessage (class)

class WebSocketMessage(
    val kind: string,
    val payload: string,
    val available: bool,
    val closed: bool,
    val timedOut: bool,
    val error: string
)

Holds an assembled message's kind and payload or the stream outcome when none is available.

WebSocketConnection (class)

resource class WebSocketConnection(
    val stream: TcpConnection,
    var buffered: string,
    val expectMasked: bool,
    val outgoingMask: string,
    var closed: bool,
    var fragmentedOpcode: int,
    var fragmentedPayload: string,
    // How long one frame may take from its first byte to its last (0: no limit), and how long a write of one frame may take (0: no limit); see `WebSocketServeOptions`.
    var frameTimeoutMilliseconds: int = 120000,
    var writeTimeoutMilliseconds: int = 30000,
    // How long a read waits for the next byte when the caller gives no timeout (it restarts on every byte); 0 is no limit.
    var idleTimeoutMilliseconds: int = 30000
)

Owns a WebSocket transport and tracks pending frame and fragmented message bytes.

WebSocketServeOptions (class)

class WebSocketServeOptions(
    val http: HttpServeOptions,
    val idleTimeoutMilliseconds: int = 30000,
    val frameTimeoutMilliseconds: int = 120000,
    val writeTimeoutMilliseconds: int = 30000
)

Sets the deadlines of the blocking WebSocket server (serveWebSockets, serveWebSocketOnce), in milliseconds, 0 turning one off. http bounds the request that asks for the upgrade (HttpServeOptions: a first byte, a head and a request that must arrive in time, the response written in time, the head and message size limits). After the upgrade idleTimeoutMilliseconds is how long a read waits for the next byte (it restarts on every byte, so it does not end a peer that trickles), frameTimeoutMilliseconds how long one frame may take from its first byte to its last (a peer that trickles one frame), writeTimeoutMilliseconds how long one write may take (a peer that does not read). A connection that runs out of time is closed. The server runs the handler on the thread that accepts, so a session holds the server until it ends: a service on an open network wants abla/websocket/server or abla/websocket/event, which read many connections at once.

webSocketServeOptions (fun)

fun webSocketServeOptions(): WebSocketServeOptions

The options serveWebSockets uses: the defaults of httpServeOptions() for the upgrade request, 30 s idle, 120 s per frame, 30 s per write.

WebSocketRoute (class)

class WebSocketRoute(
    val pattern: string,
    val handler: (HttpRequest, var: WebSocketConnection) -> void
)

Associates an HTTP path pattern with its WebSocket handler.

WebSocketRouter (class)

class WebSocketRouter(val routes: array<WebSocketRoute>)

Holds the routes used to dispatch upgraded requests.

webSocketCloseCodeValid (fun)

fun webSocketCloseCodeValid(code: int): bool

Returns true if the close code is permitted on the wire.

webSocketMask (fun)

fun webSocketMask(payload: string, key: string): string

Returns the payload XORed with the repeating four-byte key, or empty text if the key length is invalid.

webSocketEncodeFrame (fun)

fun webSocketEncodeFrame(
    payload: string,
    opcode: int = 1,
    final: bool = true,
    maskingKey: string = ""
): string

Encodes payload with the given opcode and final flag. maskingKey is empty for no mask or has exactly four bytes. Returns an empty string for invalid opcode or mask, or a control frame that is fragmented or longer than 125 bytes.

webSocketDecodeFrame (fun)

fun webSocketDecodeFrame(
    input: string,
    maximumPayload: int = 1048576,
    requireMasked: bool = false
): WebSocketFrameDecode

Decodes one frame from the start of input, capping its payload at maximumPayload and requiring masking when requireMasked is true. The result separates a complete frame, incomplete input, and a protocol error.

webSocketUtf8Valid (fun)

fun webSocketUtf8Valid(value: string): bool

Returns true if the input is a valid UTF-8 encoding.

webSocketAccept (fun)

fun webSocketAccept(key: string): string

Returns the Sec-WebSocket-Accept value derived from the client's key.

webSocketHeaderContainsToken (fun)

fun webSocketHeaderContainsToken(value: string, wanted: string): bool

Returns true if the comma-separated header contains the wanted token, ignoring ASCII case and adjacent whitespace.

webSocketIsUpgradeRequest (fun)

fun webSocketIsUpgradeRequest(request: HttpRequest): bool

Returns true for a WebSocket version 13 GET upgrade with the required headers and a valid 16-byte nonce.

webSocketUpgradeResponse (fun)

fun webSocketUpgradeResponse(request: HttpRequest): HttpResponse

Returns a 101 switching-protocols response for a valid request or a 400 error otherwise.

webSocketConnection (fun)

fun webSocketConnection(
    own stream: TcpConnection,
    expectMasked: bool = true,
    outgoingMask: string = ""
): WebSocketConnection

Takes ownership of stream. Select expectMasked for client frames and provide a four-byte outgoingMask to mask writes, or an empty string for unmasked writes.

WebSocketConnection.sendFrame (extension fun)

fun WebSocketConnection.sendFrame(
    payload: string,
    opcode: int,
    final: bool = true
): bool

Sends payload with the given opcode and final flag. Returns false for a closed connection, an invalid frame, or an incomplete socket write.

WebSocketConnection.sendText (extension fun)

fun WebSocketConnection.sendText(payload: string): bool

Sends UTF-8 payload as a text message. Returns false on invalid UTF-8 or a failed frame send.

WebSocketConnection.sendBinary (extension fun)

fun WebSocketConnection.sendBinary(payload: string): bool

Sends payload as a binary message. Returns false if the frame cannot be sent.

WebSocketConnection.sendPing (extension fun)

fun WebSocketConnection.sendPing(payload: string = ""): bool

Sends a ping carrying payload (at most 125 bytes). Returns false if the frame cannot be sent.

WebSocketConnection.sendPong (extension fun)

fun WebSocketConnection.sendPong(payload: string = ""): bool

Sends a pong carrying payload (at most 125 bytes). Returns false if the frame cannot be sent.

webSocketClosePayload (fun)

fun webSocketClosePayload(code: int, reason: string): string

Returns the wire close payload, or empty text for an invalid code, invalid UTF-8, or a reason over 123 bytes.

WebSocketConnection.close (extension fun)

fun WebSocketConnection.close(
    code: int = 1000,
    reason: string = ""
): bool

Attempts to send a close frame with code and UTF-8 reason, then closes the underlying stream even if the frame fails. Returns true when already closed or when the frame was sent.

WebSocketConnection.readFrame (extension fun)

fun WebSocketConnection.readFrame(
    maximumPayload: int = 1048576,
    timeoutMilliseconds: int = -1
): WebSocketReadResult

Returns the next bounded frame or the closure, timeout, or protocol error encountered while reading.

WebSocketConnection.readMessage (extension fun)

fun WebSocketConnection.readMessage(
    maximumPayload: int = 1048576,
    timeoutMilliseconds: int = -1
): WebSocketMessage

Reads a complete text or binary message, handling fragmentation and control frames. Uses maximumPayload to bound frame and message size and timeoutMilliseconds for reads. The result distinguishes a message, closure, timeout, and protocol or I/O errors.

webSocketRouter (fun)

fun webSocketRouter(): WebSocketRouter

Creates an empty WebSocket router.

WebSocketRouter.route (extension fun)

fun WebSocketRouter.route(
    pattern: string,
    handler: (HttpRequest, var: WebSocketConnection) -> void
): WebSocketRouter

Appends a route for the path pattern and its connection handler. Returns this router for chaining.

WebSocketRouter.find (extension fun)

fun WebSocketRouter.find(request: HttpRequest): int

Returns the first registered route index matching the path of request, or -1 if absent.

WebSocketRouter.routedRequest (extension fun)

fun WebSocketRouter.routedRequest(
    routeIndex: int,
    request: HttpRequest
): HttpRequest

Returns the request with path parameters captured by the route at a valid index.

serveWebSocketOnceWith (fun)

fun serveWebSocketOnceWith(
    router: WebSocketRouter,
    var listener: TcpListener,
    options: WebSocketServeOptions
): bool

Accepts one client from listener and uses router to handle a WebSocket upgrade within the deadlines of options. Returns whether the upgrade was handled.

serveWebSocketOnce (fun)

fun serveWebSocketOnce(
    router: WebSocketRouter,
    var listener: TcpListener,
    maximumHeader: int = 65536,
    maximumMessage: int = 1048576
): bool

Accepts one client from listener and uses router to handle a WebSocket upgrade. maximumHeader and maximumMessage bound input sizes. The deadlines are those of webSocketServeOptions(). Returns whether the upgrade was handled.

serveWebSocketsOnListenerWith (fun)

fun serveWebSocketsOnListenerWith(
    router: WebSocketRouter,
    var listener: TcpListener,
    options: WebSocketServeOptions
): int

Serves WebSocket upgrades from listener through router within the deadlines of options until graceful shutdown, and returns zero on graceful shutdown.

serveWebSocketsWith (fun)

fun serveWebSocketsWith(
    router: WebSocketRouter,
    port: int,
    options: WebSocketServeOptions
): int

Listens on the given TCP port and runs the WebSocket router within the deadlines of options, returning the listener loop's status.

serveWebSockets (fun)

fun serveWebSockets(
    router: WebSocketRouter,
    port: int,
    maximumHeader: int = 65536,
    maximumMessage: int = 1048576
): int

Listens on the given TCP port and runs the WebSocket router with the deadlines of webSocketServeOptions(), returning the listener loop's status.

Built from Abla v0.2.79 source ba5d6b7d. Examples and generated signatures follow that source revision.