Class: WebSocketClient

WebSocketClient()

A client for the WebSocket protocol.

Emits the following events:

  • open, Event
  • message, MessageEvent
  • error, Event
  • close, CloseEvent

Constructor

new WebSocketClient()

The constructor is for internal use. Use the factory methods instead.

Implements:
See:

Classes

WebSocketClient

Members

(readonly) isConnected :boolean

Returns whether the client is connected.

Type:
  • boolean
Implements:

(readonly) url :string

Returns the server URL.

Type:
  • string
Implements:

Methods

(async) close(codeopt, reasonopt)

Closes the connection.

If a code is provided, also a reason should be provided.

Parameters:
Name Type Attributes Description
code number <optional>

An optional code.

reason string <optional>

An optional reason.

Implements:

(async) connect(url)

Connects to the server.

Parameters:
Name Type Description
url URL | string

The server URL to connect to.

Implements:

(async) send(message)

Sends a message to the server.

Parameters:
Name Type Description
message string

The message to send.

Implements:

simulateClose(code, reason)

Simulates a close event.

Parameters:
Name Type Description
code number

An optional code.

reason string

An optional reason.

simulateError()

Simulates an error event.

simulateHeartbeat()

Simulates a heartbeat.

simulateMessage(message)

Simulates a message event from the server.

Parameters:
Name Type Description
message string

The message to receive.

trackMessageSent() → {OutputTracker}

Returns a tracker for messages sent.

Returns:

A new output tracker.

Type
OutputTracker

(static) create(options) → {WebSocketClient}

Creates a WebSocket client.

Parameters:
Name Type Description
options object
Properties
Name Type Attributes Default Description
heartbeat number <optional>
30000

The heartbeat interval i milliseconds. A value <= 0 disables the heartbeat.

Returns:

A new WebSocket client.

Type
WebSocketClient

(static) createNull(options) → {WebSocketClient}

Creates a nulled WebSocket client.

Parameters:
Name Type Description
options object
Properties
Name Type Attributes Default Description
heartbeat number <optional>
-1

The heartbeat interval in milliseconds. A value <= 0 disables the heartbeat.

Returns:

A new nulled WebSocket client.

Type
WebSocketClient