@ceeblue/web-utils
    Preparing search index...

    Class WebSocketReliable

    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.
    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 (View Summary)

    Index

    Constructors

    Properties

    log: ILog = ...

    Start a log

    a Log object with the levels of log to call

    Accessors

    • 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 opened(): boolean

      opened equals true when connection is etablished, in other word when onOpen event is fired

      Returns boolean

    • get queueing(): (string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>)[]

      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<ArrayBufferLike>)[]

    Methods

    • 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

      • Optionaloptions: { signal?: AbortSignal }
        • Optionalsignal?: 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

      • Optionaloptions: { signal?: AbortSignal }
        • Optionalsignal?: AbortSignal

          Optional AbortSignal to stop this or multiple subscriptions in same time

      Returns void

    • Send a message

      Parameters

      • message: string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>
      • queueing: boolean = false

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

      Returns WebSocketReliable

      this

    Events

    • 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