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).
- ClientSocketBase(url: string,): ClientSocketBase
_args: ClientOptions = {} No description.
- meanPingInterval: number
Mean interval of the received pings, with a weight favoring the most recent pings.
- pingWindowThreshold: number
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.
- maxReconnectTimeoutDuration: number
Maximum timeout between reconnection attempts in milliseconds.
Setting this to a negative value disables all reconnection attempts.
See also
minReconnectTimeoutDuration
- minReconnectTimeoutDuration: number
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.
- static readonly pingPayload: ArrayBuffer
Object that is used in the ping payload. May not be changed.
Socket pass-thru. Sends the specified message.
Convenience method. Sends the stringified specified data object with the added field
evt
set to the specified event string.- initializeConnection(): void
Initialize a new connection, overwriting the current
socket
. Does not close or cancel any currently existing socket connnection.- _socketClosed(e: CloseEvent): void
No description.
- _socketConnected(): void
No description.
- stopReconnectionAttempt(): void
Stop trying to reconnect immediately.
- _handleReceivedPing(): void
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.
- _handlePotentialReconnect(): void
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.
- _clearPingTimeout(): void
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,): void
callback: AvailableEventMap[K] 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,): void
callback: AvailableEventMap[K] No description.
- _addEventsAgain(): void
No description.
- invokeEvent<K extends unknown | unknown>(type: K,): void
...args: Parameters<AvailableEventMap[K]> No description.
- removeAllEvents(): void
No description.
interface ClientOptions
- pingWindowThreshold?: number
See also
ClientSocketBase.pingWindowThreshold
Default
1.25
- maxReconnectTimeoutDuration?: number
See also
ClientSocketBase.maxReconnectTimeoutDuration
Default
10000
- minReconnectTimeoutDuration?: number
See also
ClientSocketBase.minReconnectTimeoutDuration
Default
750
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
- ServerSocketBase(socket: WebSocket,): ServerSocketBase
_args: ServerOptions = {} 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.
- static readonly pingPayload: ArrayBuffer
Object that is used in the ping payload. May not be changed.
- pingInterval():
No description.
- _socketClosed(e: CloseEvent): void
No description.
- _socketConnected(): void
No description.
- _handleReceivedPing(): void
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
- stopPingImmediately(): void
Stop an ongoing ping interval immediately, without waiting for the current interval to finish.
- restartPingInterval(): void
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.
- _handlePotentialReconnect(): void
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.
- _clearPingTimeout(): void
Clears a potential previous ping timeout.
See also
_addPingTimeout
- addEventListener<K extends unknown | unknown>(type: K,): void
callback: AvailableEventMap[K] 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,): void
callback: AvailableEventMap[K] No description.
- _addEventsAgain(): void
No description.
- invokeEvent<K extends unknown | unknown>(type: K,): void
...args: Parameters<AvailableEventMap[K]> No description.
- removeAllEvents(): void
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 & {}
No description.
class SocketBase
Base class that should be extended. Features base functionality such as the event and ping handlers.
- SocketBase(socket: | WebSocket): SocketBase
No description.
- isTimedOut: boolean
Denotes whether the socket is in a timed out state.
- 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.
- _handleReceivedPing(): void
Called whenever a ping has been received. Can be extended with additional functionality.
- _handlePotentialReconnect(): void
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.
- _clearPingTimeout(): void
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,): void
callback: AvailableEventMap[K] 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,): void
callback: AvailableEventMap[K] No description.
- _addEventsAgain(): void
No description.
- invokeEvent<K extends unknown | unknown>(type: K,): void
...args: Parameters<AvailableEventMap[K]> No description.
- removeAllEvents(): void
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.