module ClientSocketBase

class ClientSocketBase extends SocketBase

Client implementation of the SocketBase.

The capabilities of this class include receiving pings from the server and immediately replying with a pong. Additionally, as soon as the socket closes (for example due to a lost connection), a reconnection attempt is started using the configurable min and max timings (see below).

No description.

Mean interval of the received pings, with a weight favoring the most recent pings.

Denotes the ratio to the meanPingInterval (so the mean interval between two pings) in which a ping must be received before a timeout is assumed.

Example

The value is multiplied with {@link meanPingInterval}, so a value of 2 would
mean that a ping must be received within double the mean ping interval.

Maximum timeout between reconnection attempts in milliseconds.

Setting this to a negative value disables all reconnection attempts.

See also minReconnectTimeoutDuration

Minimum timeout between reconnection attempts in milliseconds.

After each failed reconnection attempt, the effective timeout is doubled, reaching a cap at maxReconnectTimeoutDuration.

socketURL: string

Used socket URL. Changing it will only be reflected after a reconnect.

isTimedOut: boolean

Denotes whether the socket is in a timed out state.

No description.

static readonly pingPayload: ArrayBuffer

Object that is used in the ping payload. May not be changed.

send(
message: string | Blob | ArrayBufferLike | ArrayBufferView<
ArrayBufferLike
>
): void

Socket pass-thru. Sends the specified message.

sendEvent(
eventType: string,
data: Record<any, any> = {}
): void

Convenience method. Sends the stringified specified data object with the added field evt set to the specified event string.

Initialize a new connection, overwriting the current socket. Does not close or cancel any currently existing socket connnection.

No description.

No description.

Stop trying to reconnect immediately.

Called whenever a ping has been received. Can be extended with additional functionality.

_missedPing(): void

Called internally whenever no pong has been received in the required time frame after a ping.

Handles reconnection operations in case the socket is timed out. Is called whenever a message is received.

_addPingTimeout(duration: number): void

Clears a potential previous ping timeout and starts a new one. A ping timeout is the time frame in which a pong must be received.

Must be called externally or from a super class.

Clears a potential previous ping timeout.

See also _addPingTimeout

sendPing(): void

Sends a ping. A ping is always the same object defined in pingPayload.

addEventListener<K extends unknown | unknown>(
type: K,
callback: AvailableEventMap[K]
): void

Add an event listener to the socket. Accepts socket events (pass-thru) and custom events.

See also CustomEventMap

removeEventListener<K extends unknown | unknown>(
type: K,
callback: AvailableEventMap[K]
): void

No description.

No description.

invokeEvent<K extends unknown | unknown>(
type: K,
...args: Parameters<AvailableEventMap[K]>
): void

No description.

No description.

interface ClientOptions

module ServerSocketBase

class ServerSocketBase extends SocketBase

Server implementation of the SocketBase.

This class is responsible for sending pings and reacting with a timeout once a pong has not been received in the given threshold.

See also SocketBase

No description.

pingTimeout: number

Amount of time in milliseconds that is waited for a ping response. If no response comes within this window, a timeout event will be invoked and a reconnection attempt is started.

isTimedOut: boolean

Denotes whether the socket is in a timed out state.

No description.

static readonly pingPayload: ArrayBuffer

Object that is used in the ping payload. May not be changed.

No description.

No description.

No description.

Called whenever a ping has been received. Can be extended with additional functionality.

sendPing(): void

Sends a ping as per the corresponding super method and activates a timeout that will send a _timeout event when no pong has been received in time.

See also SocketBase.sendPing

Stop an ongoing ping interval immediately, without waiting for the current interval to finish.

Stop an ongoing ping interval immediately and immediately start it again.

_missedPing(): void

Called internally whenever no pong has been received in the required time frame after a ping.

Handles reconnection operations in case the socket is timed out. Is called whenever a message is received.

_addPingTimeout(duration: number): void

Clears a potential previous ping timeout and starts a new one. A ping timeout is the time frame in which a pong must be received.

Must be called externally or from a super class.

Clears a potential previous ping timeout.

See also _addPingTimeout

addEventListener<K extends unknown | unknown>(
type: K,
callback: AvailableEventMap[K]
): void

Add an event listener to the socket. Accepts socket events (pass-thru) and custom events.

See also CustomEventMap

removeEventListener<K extends unknown | unknown>(
type: K,
callback: AvailableEventMap[K]
): void

No description.

No description.

invokeEvent<K extends unknown | unknown>(
type: K,
...args: Parameters<AvailableEventMap[K]>
): void

No description.

No description.

interface ServerOptions

pingInterval?: number

See also ServerSocketBase.pingInterval

Default

0
pingTimeout?: number

See also ServerSocketBase.pingTimeout

Default

3000

module SocketBase

type AvailableEventMap: CustomEventMap & {
[K in keyof WebSocketEventMap]: (this: WebSocket, ev: WebSocketEventMap[K]) => any
}

No description.

class SocketBase

Base class that should be extended. Features base functionality such as the event and ping handlers.

No description.

isTimedOut: boolean

Denotes whether the socket is in a timed out state.

No description.

static readonly pingPayload: ArrayBuffer

Object that is used in the ping payload. May not be changed.

_missedPing(): void

Called internally whenever no pong has been received in the required time frame after a ping.

Called whenever a ping has been received. Can be extended with additional functionality.

Handles reconnection operations in case the socket is timed out. Is called whenever a message is received.

_addPingTimeout(duration: number): void

Clears a potential previous ping timeout and starts a new one. A ping timeout is the time frame in which a pong must be received.

Must be called externally or from a super class.

Clears a potential previous ping timeout.

See also _addPingTimeout

sendPing(): void

Sends a ping. A ping is always the same object defined in pingPayload.

addEventListener<K extends unknown | unknown>(
type: K,
callback: AvailableEventMap[K]
): void

Add an event listener to the socket. Accepts socket events (pass-thru) and custom events.

See also CustomEventMap

removeEventListener<K extends unknown | unknown>(
type: K,
callback: AvailableEventMap[K]
): void

No description.

No description.

invokeEvent<K extends unknown | unknown>(
type: K,
...args: Parameters<AvailableEventMap[K]>
): void

No description.

No description.

interface CustomEventMap

Available custom events and their callbacks.

_timeout: () => any

No description.

_reconnect: () => any

No description.

_sentPing: () => any

No description.

_receivedPing: () => any

No description.