Skip to main content

CloudWebSocketClient

CloudWebSocketClient is the default NetworkClient, carrying protocol messages to the cloud node over a WebSocket.

Import

import { CloudWebSocketClient } from '@silencelaboratories/silent-shard-sdk';

Usage

 const couldClient = new CloudWebSocketClient(
'CLOUD_NODE_URL',
false, // boolean indicates if the connection is `WSS` or `WS`
);

Implements

Constructors

new CloudWebSocketClient()

new CloudWebSocketClient(url, isSecure): CloudWebSocketClient

Constructor

Parameters

url

string

The URI of the cloud node.

isSecure

boolean

If the connection is TLS // WSS true / WS false.

Returns

CloudWebSocketClient

Properties

_closeEvent

_closeEvent: null | WebSocketCloseEvent = null

The close event that was received from the WebSocket server.


_closeWaitCompleter

_closeWaitCompleter: Promise<undefined | CloseReason>

A promise that resolves when the WebSocket closes.


_receiveCallbacksQueue

_receiveCallbacksQueue: object[] = []

A queue of pending read() callbacks. A way to make WebSocket client asynchronous.

reject()

reject: (reason) => void

Parameters
reason

any

Returns

void

resolve()

resolve: (data) => void

Parameters
data

any

Returns

void


_receiveDataQueue

_receiveDataQueue: any[] = []

A queue of received WebSocket messages.


_socket

_socket: null | WebSocket = null

The WebSocket client

Accessors

connected

Get Signature

get connected(): boolean

Checks if the WebSocket client is connected.

Returns

boolean

True if the WebSocket is connected, false otherwise.


isSecure

Get Signature

get isSecure(): boolean

Returns whether the connection over the TLS or not.

Returns

boolean

True if the connection is secure (WSS), false otherwise (WS).


url

Get Signature

get url(): string

Returns the URL of the cloud node.

Returns

string

The URL of the cloud node.

Methods

close()

close(code?, reason?): Promise<unknown>

Closes the connection to the cloud node.

Parameters

code?

number

reason?

string

Returns

Promise<unknown>

A promise that resolves when the connection is closed.

Implementation of

NetworkClient.close


connect()

connect(action, options?): Promise<void>

Opens a channel for one protocol step.

Parameters

action

NetworkAction

The step to perform. Must be a WebSocketAction.

options?

CloudWebSocketOptions

WebSocket-specific options, such as headers.

Returns

Promise<void>

A promise that resolves when the connection is established.

Implementation of

NetworkClient.connect


read()

read(): Promise<Uint8Array>

Reads the next message from the cloud node.

Returns

Promise<Uint8Array>

A promise that resolves to the received bytes.

Implementation of

NetworkClient.read


reset()

reset(): void

Resets the client, clearing any existing connection.

Returns

void

Implementation of

NetworkClient.reset


send()

send(data): Promise<void>

Sends a binary message to the cloud node.

Parameters

data

Uint8Array

The bytes to send.

Returns

Promise<void>

A promise that resolves when the message is sent.

Implementation of

NetworkClient.send


sendPostRequest()

sendPostRequest(path, body, headers?): Promise<Uint8Array>

Issues a request-response call against the cloud node, for the protocol steps that run over HTTP rather than the WebSocket.

Parameters

path

string

Path appended to the configured base URL.

body

Uint8Array

The request body.

headers?

Record<string, string>

Additional request headers.

Returns

Promise<Uint8Array>

A promise that resolves to the response bytes.

Implementation of

PostRequestCapable.sendPostRequest


sendText()

sendText(data): Promise<void>

Sends an 'utf-8' text message to the cloud node.

Parameters

data

string

An 'utf-8' text message to send.

Returns

Promise<void>

A promise that resolves when the message is sent.

Implementation of

NetworkClient.sendText


waitRemoteClose()

waitRemoteClose(): Promise<undefined | CloseReason>

Waits for the cloud node to close the connection.

Returns

Promise<undefined | CloseReason>

A promise that resolves when the remote side closes.

Implementation of

NetworkClient.waitRemoteClose