Skip to main content
Module

x/cav/mod.ts>Socket

A server framework for Deno
Go to Latest
interface Socket
Re-export
import { type Socket } from "https://deno.land/x/cav@0.0.21/mod.ts";

Cav's WebSocket wrapper interface.

Type Parameters

optional
Send = unknown
optional
Message = unknown

Properties

The raw WebSocket instance.

send: (data: Send) => void

Send data to the connected party. The data provided is serialized using the top-level serialize() function.

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.

Methods

on(type: "open", cb: SocketListener<"open">): void

Register an event listener for the "open" event, which is fired when the web socket connection is established. The socket must be opened before any data can be sent.

on(type: "close", cb: SocketListener<"close">): void

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

on(type: "message", cb: SocketListener<"message", Message>): void

Register an event listener for the "message" event, which is fired every time a message is received from the connected party. The message received is deserialized and made available on the "message" property assigned to the event.

on(type: "error", cb: SocketListener<"error">): void

Register an event listener for the "error" event, which is fired when the connection has been closed due to an error.

off(type?:
| "open"
| "close"
| "message"
| "error"
, cb?: (ev: Event) => void | Promise<void>
): 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.