Options
All
  • Public
  • Public/Protected
  • All
Menu

The Dam simulate a Websocket Opening and Closing Example of use include simulating a connection opening after an authentication

example

const dam = new Dam(ws);
expect(dam.send("test")).to.throw;
dam.status = Dam.OPEN;
\// the open event is also dispatched
expect(dam.send("test")).to.not.throw;

Hierarchy

Implements

  • WebSocket

Index

Constructors

constructor

  • new Dam(ws: WebSocket): Dam

Properties

Readonly CLOSED

CLOSED: number = WebSocket.CLOSED

The connection is closed or couldn't be opened.

Readonly CLOSING

CLOSING: number = WebSocket.CLOSING

The connection is in the process of closing.

Readonly CONNECTING

CONNECTING: number = WebSocket.CONNECTING

The connection is not yet open.

Readonly OPEN

OPEN: number = WebSocket.OPEN

The connection is open and ready to communicate.

Protected _readyState

_readyState: number = WebSocket.CONNECTING

binaryType

binaryType: "blob" | "arraybuffer"

A string indicating the type of binary data being transmitted by the connection. This should be either "blob" if DOM Blob objects are being used or "arraybuffer" if ArrayBuffer objects are being used.

Protected closing

closing: boolean = false

Protected listeners

listeners: Dict<keyof IHydratedWebSocketEventMap, Array<{ listener: (this: WebSocket, ev: IHydratedWebSocketEventMap[keyof IHydratedWebSocketEventMap]) => any; useCapture?: boolean }>> = new Dict()

Protected ws

ws: WebSocket

Static CLOSED

CLOSED: string = "CLOSED"

The dam is closed and will stop the messages

Static OPEN

OPEN: string = "OPEN"

The dam is open and will let messages pass through

Accessors

bufferedAmount

  • get bufferedAmount(): number
  • The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. This value resets to zero once all queued data has been sent. This value does not reset to zero when the connection is closed; if you keep calling send(), this will continue to climb. Read only

    Returns number

extensions

  • get extensions(): string
  • The extensions selected by the server. This is currently only the empty string or a list of extensions as negotiated by the connection.

    Returns string

onclose

  • get onclose(): (ev: CloseEvent) => any
  • set onclose(f: (ev: CloseEvent) => any): void
  • An event listener to be called when the WebSocket connection's readyState changes to CLOSED. The listener receives a CloseEvent named "close".

    Returns (ev: CloseEvent) => any

      • (ev: CloseEvent): any
      • Parameters

        • ev: CloseEvent

        Returns any

  • An event listener to be called when the WebSocket connection's readyState changes to CLOSED. The listener receives a CloseEvent named "close".

    Parameters

    • f: (ev: CloseEvent) => any
        • (ev: CloseEvent): any
        • Parameters

          • ev: CloseEvent

          Returns any

    Returns void

onerror

  • get onerror(): (ev: Event) => any
  • set onerror(f: (ev: Event) => any): void
  • An event listener to be called when an error occurs. This is a simple event named "error".

    Returns (ev: Event) => any

      • (ev: Event): any
      • Parameters

        • ev: Event

        Returns any

  • An event listener to be called when an error occurs. This is a simple event named "error".

    Parameters

    • f: (ev: Event) => any
        • (ev: Event): any
        • Parameters

          • ev: Event

          Returns any

    Returns void

onmessage

  • get onmessage(): (ev: MessageEvent) => any
  • set onmessage(f: (ev: MessageEvent) => any): void
  • An event listener to be called when a message is received from the server. The listener receives a MessageEvent named "message".

    Returns (ev: MessageEvent) => any

      • (ev: MessageEvent): any
      • Parameters

        • ev: MessageEvent

        Returns any

  • An event listener to be called when a message is received from the server. The listener receives a MessageEvent named "message".

    Parameters

    • f: (ev: MessageEvent) => any
        • (ev: MessageEvent): any
        • Parameters

          • ev: MessageEvent

          Returns any

    Returns void

onopen

  • get onopen(): (ev: Event) => any
  • set onopen(f: (ev: Event) => any): void
  • An event listener to be called when the WebSocket connection's readyState changes to OPEN; this indicates that the connection is ready to send and receive data. The event is a simple one with the name "open".

    Returns (ev: Event) => any

      • (ev: Event): any
      • Parameters

        • ev: Event

        Returns any

  • An event listener to be called when the WebSocket connection's readyState changes to OPEN; this indicates that the connection is ready to send and receive data. The event is a simple one with the name "open".

    Parameters

    • f: (ev: Event) => any
        • (ev: Event): any
        • Parameters

          • ev: Event

          Returns any

    Returns void

protocol

  • get protocol(): string

readyState

  • get readyState(): number
  • Compatible with the WebSocket readyState, combining the status of the Dam and the ready state of the underlying WebSocket

    Returns number

    WebSocket constants (CONNECTING, OPEN, CLOSING, CLOSED)

status

  • get status(): "OPEN" | "CLOSED"
  • set status(value: "OPEN" | "CLOSED"): void

url

  • get url(): string
  • The URL as resolved by the constructor. This is always an absolute URL. Read only.

    Returns string

Methods

addEventListener

  • addEventListener<K>(type: K, listener: (this: WebSocket, ev: IHydratedWebSocketEventMap[K]) => any, useCapture?: boolean): void
  • Register an event handler of a specific event type on the EventTarget.

    Type parameters

    Parameters

    • type: K
    • listener: (this: WebSocket, ev: IHydratedWebSocketEventMap[K]) => any
        • (this: WebSocket, ev: IHydratedWebSocketEventMap[K]): any
        • Parameters

          • this: WebSocket
          • ev: IHydratedWebSocketEventMap[K]

          Returns any

    • Optional useCapture: boolean

    Returns void

close

  • close(code?: number, reason?: string): void
  • Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

    Parameters

    • Default value code: number = 1000
    • Optional reason: string

    Returns void

dispatchEvent

  • dispatchEvent(evt: Event): boolean
  • Dispatch an event, some event are delayed if the Dam is CLOSED

    Parameters

    • evt: Event

    Returns boolean

    The return value is false if event is cancelable and at least one of the event handlers which handled this event called Event.preventDefault(). Otherwise it returns true.

Protected forwardEvents

  • forwardEvents<K>(list?: K[]): void

Protected getOnmessage

  • getOnmessage(): (ev: MessageEvent) => any

Protected getReadyState

  • getReadyState(): number

removeEventListener

  • removeEventListener<K>(type: K, listener: (this: WebSocket, ev: IHydratedWebSocketEventMap[K]) => any, useCapture?: boolean): void
  • Removes an event listener from the EventTarget.

    Type parameters

    Parameters

    • type: K
    • listener: (this: WebSocket, ev: IHydratedWebSocketEventMap[K]) => any
        • (this: WebSocket, ev: IHydratedWebSocketEventMap[K]): any
        • Parameters

          • this: WebSocket
          • ev: IHydratedWebSocketEventMap[K]

          Returns any

    • Optional useCapture: boolean

    Returns void

send

  • send(data: string | ArrayBuffer | Blob | ArrayBufferView): void
  • Same as the send method of the WebSocket except that this will throw an error if the Dam is closed

    Parameters

    • data: string | ArrayBuffer | Blob | ArrayBufferView

      The data to send to the server. It may be one of the following types:

      • USVString A text string. The string is added to the buffer in UTF-8 format, and the value of bufferedAmount is increased by the number of bytes required to represent the UTF-8 string.
      • ArrayBuffer You can send the underlying binary data used by a typed array object; its binary data contents are queued in the buffer, increasing the value of bufferedAmount by the requisite number of bytes.
      • Blob Specifying a Blob enqueues the blob's raw data to be transmitted in a binary frame. The value of bufferedAmount is increased by the byte size of that raw data.
      • ArrayBufferView You can send any JavaScript typed array object as a binary frame; its binary data contents are queued in the buffer, increasing the value of bufferedAmount by the requisite number of bytes.

    Returns void

Protected setOnmessage

  • setOnmessage(f: (ev: MessageEvent) => any): void
  • Parameters

    • f: (ev: MessageEvent) => any
        • (ev: MessageEvent): any
        • Parameters

          • ev: MessageEvent

          Returns any

    Returns void

Protected stopForwardingEvents

  • stopForwardingEvents(): void

Legend

  • Constructor
  • Property
  • Method
  • Accessor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Property
  • Method
  • Protected property
  • Protected method
  • Static property
  • Static method

Generated using TypeDoc