@ceeblue/webrtc-client
    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

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

      Parameters

      • Optionalurl: string | URL

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

      • Optionalprotocols: string | string[]

      Returns WebSocketReliable

    Properties

    log: ILog

    Start a log

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

    • 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: "close" | "Close" | "Open" | "open" | "Message" | "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: "close" | "Close" | "Open" | "open" | "Message" | "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: "close" | "Close" | "Open" | "open" | "Message" | "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

    • Open a WebSocket connection

      Parameters

      • url: string | URL

        url of the websocket endpoint

      • Optionalprotocols: string | string[]

      Returns this

      this

    • Send a message

      Parameters

      • message: string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>
      • Optionalqueueing: boolean

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

      Returns this

      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