A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.

Hierarchy

  • Websocket

Constructors

  • Creates a new websocket.

    Parameters

    • url: string

      to connect to.

    • Optional protocols: string | string[]

      optional protocols to use.

    • Optional options: WebsocketOptions

      optional options to use.

    Returns Websocket

Properties

_closedByUser: boolean = false
_lastConnection?: Date
_options: WebsocketOptions & Required<Pick<WebsocketOptions, "retry" | "listeners">>
_protocols?: string | string[]
_underlyingWebsocket: WebSocket
_url: string
retryTimeout?: Timeout

Accessors

  • get backoff(): undefined | Backoff
  • Getter for the backoff.

    Returns undefined | Backoff

    the backoff, or undefined if none was provided.

  • get buffer(): undefined | WebsocketBuffer<string | ArrayBufferLike | ArrayBufferView | Blob>
  • Getter for the buffer.

    Returns undefined | WebsocketBuffer<string | ArrayBufferLike | ArrayBufferView | Blob>

    the buffer, or undefined if none was provided.

  • get closedByUser(): boolean
  • Whether the websocket was closed by the user. A websocket is closed by the user if the close().

    Returns boolean

    true if the websocket was closed by the user, false otherwise.

  • get instantReconnect(): undefined | boolean
  • Getter for the instantReconnect.

    Returns undefined | boolean

    the instantReconnect, or undefined if none was provided.

  • get lastConnection(): undefined | Date
  • Getter for the last 'open' event, e.g. the last time the websocket was connected.

    Returns undefined | Date

    the last 'open' event, or undefined if the websocket was never connected.

  • get maxRetries(): undefined | number
  • Getter for the maxRetries.

    Returns undefined | number

    the maxRetries, or undefined if none was provided (no limit).

  • get protocols(): undefined | string | string[]
  • Getter for the protocols.

    Returns undefined | string | string[]

    the protocols, or undefined if none were provided.

  • get url(): string
  • Getter for the url.

    Returns string

    the url.

Methods

  • Cancels the scheduled connection-retry, if there is one.

    Returns void

  • Removes all event listeners from the browser-native websocket and closes it.

    Returns void

  • Dispatch an event to all listeners of the given event-type.

    Type Parameters

    Parameters

    Returns void

  • Handles the 'close' event of the browser-native websocket.

    Parameters

    • event: CloseEvent

      to handle.

    Returns void

  • Handles the 'error' event of the browser-native websocket.

    Parameters

    • event: Event

      to handle.

    Returns void

  • Handles the given event by dispatching it to all listeners of the given event-type.

    Type Parameters

    Parameters

    Returns void

  • Handles the 'message' event of the browser-native websocket.

    Parameters

    • event: MessageEvent<any>

      to handle.

    Returns void

  • Handles the 'open' event of the browser-native websocket.

    Parameters

    • event: Event

      to handle.

    Returns void

  • Removes one or more event listener for the given event-type that match the given listener and options.

    Type Parameters

    Parameters

    Returns void

  • Schedules a connection-retry if there is a backoff defined and the websocket was not closed by the user.

    Returns void

  • Sends data over the websocket.

    If the websocket is not connected and a buffer was provided on creation, the data will be added to the buffer. If no buffer was provided or the websocket was closed by the user, the data will be dropped.

    Parameters

    • data: string | ArrayBufferLike | ArrayBufferView | Blob

      to send.

    Returns void

  • Sends buffered data if there is a buffer defined.

    Returns void

  • Creates a new browser-native websocket and connects it to the given URL with the given protocols and adds all event listeners to the browser-native websocket.

    Returns WebSocket

    the created browser-native websocket which is also stored in the '_underlyingWebsocket' property.

Generated using TypeDoc