Skip to main content
Module

x/cav/ws.ts>WS

A server framework for Deno
Go to Latest
interface WS
import { type WS } from "https://deno.land/x/cav@0.2.0-alpha.7/ws.ts";

Isomorphic WebSocket interface with JSON serialization and typed messages.

Type Parameters

optional
Send = any
optional
Recv = any

Properties

The raw WebSocket instance.

send: (data: Send) => void

Send data to the connected party. The data provided is serialized to JSON before being sent.

close: (code?: number, reason?: string) => void

Closes the web socket connection. An optional code and reason may be provided, and will be available to all "close" event listeners.

optional
onopen: WSOpenListener

Listener for the "open" event, triggered when the web socket connection is established. The socket can't send data until the open event occurs.

optional
onclose: WSCloseListener

Listener for the "close" event, triggered when the web socket connection is ended.

optional
onmessage: WSMessageListener<Recv>

Listener for the "message" event, triggered whenever a message is received from the connected party. The message received is deserialized to the event's "message" property.

optional
onerror: WSErrorListener

Listener for the "error" event, triggered when the connection has been closed due to an error or when received/sent data couldn't be deserialized/parsed.

Methods

on(type: "open", cb: WSOpenListener): void

Register an event listener for the "open" event, triggered when the web socket connection is established. The socket can't send data until the open event occurs.

on(type: "close", cb: WSCloseListener): void

Register an event listener for the "close" event, triggered when the web socket connection is ended.

on(type: "message", cb: WSMessageListener<Recv>): void

Register an event listener for the "message" event, triggered whenever a message is received from the connected party. The message received is deserialized before the listener is called.

on(type: "error", cb: WSErrorListener): void

Register an event listener for the "error" event, triggered when the connection has been closed due to an error or when an error is thrown inside one of the event listeners. This is also the event triggered when the server sends back an HttpError due to invalid input.

off(type?:
| "open"
| "close"
| "message"
| "error"
, cb?: (...a: any[]) => any
): void

Unregister an event listener for a particular event type. If no listener is provided, all listeners for that event type will be unregistered. If the event type is also omitted, all listeners for the web socket will be unregistered.