Skip to content

ServerWebSocket

1 min read

Import: import system.webapi;

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.

ServerWebSocket

A WebSocket connection offered to a @websocket route. The handshake stays open until the handler calls accept; closing the socket first, or returning without accepting it, answers the handshake with 403 Forbidden.

public class ServerWebSocket

accept

Completes the WebSocket handshake with 101 Switching Protocols. Call it once, after any checks on the handshake request such as its Origin header; messages can be sent and received only after it succeeds. Fails when the request is not a valid WebSocket upgrade, the client disconnected, or the socket was already accepted or closed.

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

send_text

Sends one 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>>

receive

Receives one complete message.

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

messages

Iterates messages through the final Close message.

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

close

Sends a normal closing handshake; before accept, rejects the connection with 403 Forbidden like close_with.

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

close_with

Sends a closing handshake with a code and reason. Before accept, this rejects the connection instead: the handshake is answered with 403 Forbidden when the handler returns, and code and reason are not sent. Use code 1008 (policy violation) for that case.

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