Skip to content

WebSocket

1 min read

Import: import system.net;

Read the usage guide for examples and common tasks. This reference lists the public declarations for this part of the library. See Reading API signatures for parameter and result notation.

WebSocket

An owned RFC 6455 WebSocket, available on native, Node and browser hosts.

public class WebSocket

connect

Opens ws:// or wss:// with an optional deadline and inherited cancellation.

public static async fun connect(
    url: string,
    timeout_ms: int = 0,
    token: Option<CancellationToken> = Option.None
): Result<WebSocket, IoError>

send_text

Sends one UTF-8 text message.

public fun send_text(
    text: string,
    token: Option<CancellationToken> = Option.None
): Future<Result<Unit, IoError>>

send_binary

Sends one binary message.

public fun send_binary(
    bytes: byte[],
    token: Option<CancellationToken> = Option.None
): Future<Result<Unit, IoError>>

close

Sends a normal closing handshake.

public fun close(token: Option<CancellationToken> = Option.None): Future<Result<Unit, IoError>>

close_with

Sends a validated close code and UTF-8 reason.

public fun close_with(
    code: int,
    reason: string,
    token: Option<CancellationToken> = Option.None
): Future<Result<Unit, IoError>>

receive

Receives one complete message. Only one receive may be pending at a time.

public fun receive(
    token: Option<CancellationToken> = Option.None
): Future<Result<WebSocketMessage, IoError>>

messages

Iterates until Close, including the final closing message. Transport errors end iteration.

public fun messages(
    token: Option<CancellationToken> = Option.None
): AsyncIterable<WebSocketMessage>