The WebSocketReliable class extends WebSocket to bring up the following improvements:

  • Fix all possible unintentional closing ways to get always a related error message, onClose(error?) event
  • Make possible message sending while connecting. Indeed no need to wait onOpen before to send message, you can open the socket and immediately send messages, it will be queue and flushs on connection etablishment
  • Make possible a delayed connection, or a reconnection. Indeed you can create an unconnected Websocket instance without passing any url argument and starts the conneciton more later with (url) | open(url) method
  • Make possible to control sending/queueing message: send method take an optional queueing=true argument to queue message rather send it, a futur call to flush will send it. Then queueing getter allows to handle the queue if need to purge it or remove some queued message. Use it all together can help to prioritize messages or control overload.

Example

const ws = new WebSocketReliable(url);
ws.onClose = (error?:string) => {
if(error) {
console.error(error);
}
// reconnection attempt every seconds
setTimeout(() => ws.open(url), 1000);
}
ws.onMessage = (message:string) => {
console.log(message);
}
ws.send('hello'); // send immediatly a hello message is possible (no need to wait onOpen)

Hierarchy

Constructors

  • Create a WebSocketReliable object, and open it if an url is passed in argument

    Parameters

    • Optional url: string | URL

      URL of the WebSocket endpoint or null to start the connection later

    • Optional protocols: string | string[]

    Returns WebSocketReliable

Properties

_closed: any
_opened: any
_queueing: any
_queueingBytes: any
_recvByteRate: any
_send: any
_sendByteRate: any
_ws?: any
log: ILog

Start a log

Param

Returns

a Log object with the levels of log to call

Accessors

  • get binaryType(): BinaryType
  • binaryType, fix binary type to arrayBuffer

    Returns BinaryType

  • get bufferedAmount(): number
  • The number of bytes of data that were queued during calls to send() but not yet transmitted to the network

    Returns number

  • get closed(): boolean
  • True when connection is closed, in other words when onClose event is fired or when WebSocketReliable is build without url (disconnected creation)

    Returns boolean

  • get extensions(): string
  • extensions negociated by the server

    Returns string

  • get opened(): boolean
  • opened equals true when connection is etablished, in other word when onOpen event is fired

    Returns boolean

  • get protocol(): string
  • protocol negociated by the server

    Returns string

  • get queueing(): (string | ArrayBuffer | ArrayBufferView)[]
  • Queued messages from a call to send() waiting to be transmit one time websocket connection opened (or with an explicit call to flush() method)

    Returns (string | ArrayBuffer | ArrayBufferView)[]

  • get recvByteRate(): number
  • Returns number

  • get sendByteRate(): number
  • Returns number

  • get url(): string
  • url of connection

    Returns string

Methods

  • Close websocket

    Parameters

    Returns void

  • Send queueing messages

    Returns void

  • Event unsubscription

    Parameters

    • name: "message" | "open" | "close" | "Close" | "Open" | "Message"

      Name of event without the 'on' prefix (ex: 'log' to 'onLog' event declared)

    • event: Function

      Unsubscriber Function, must be the one passed to on or once subscription methods

    Returns boolean

  • Event subscription

    Parameters

    • name: "message" | "open" | "close" | "Close" | "Open" | "Message"

      Name of event without the on prefix (ex: log to onLog event declared)

    • event: Function

      Subscriber Function

    • Optional options: {
          signal?: AbortSignal;
      }
      • Optional signal?: AbortSignal

        Optional AbortSignal to stop this or multiple subscriptions in same time

    Returns void

  • Event subscription only one time, once time fired it's automatically unsubscribe

    Parameters

    • name: "message" | "open" | "close" | "Close" | "Open" | "Message"

      Name of event without the on prefix (ex: log to onLog event declared)

    • event: Function

      Subscriber Function

    • Optional options: {
          signal?: AbortSignal;
      }
      • Optional signal?: AbortSignal

    Returns void

  • Open a WebSocket connection

    Parameters

    • url: string | URL

      url of the websocket endpoint

    • Optional protocols: string | string[]

    Returns WebSocketReliable

    this

  • Send a message

    Parameters

    • message: string | ArrayBuffer | ArrayBufferView
    • Optional queueing: boolean

      When set it reports the sending to a more later call to flush

    Returns WebSocketReliable

    this

Events

  • Fired on websocket close

    Parameters

    Returns void

  • Fired on message reception

    Parameters

    • message: string | ArrayBuffer

      can be binary or string. If you subscribe to the event with message as string type (and not union), it means that you know that all your messages are distributed in a string format

    Returns void

  • Fired when socket is connected

    Returns void

Generated using TypeDoc