SongGuess
    Preparing search index...

    The WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

    MDN Reference

    interface WebSocket {
        binaryType: BinaryType;
        bufferedAmount: number;
        CLOSED: 3;
        CLOSING: 2;
        CONNECTING: 0;
        extensions: string;
        onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
        onerror: ((this: WebSocket, ev: Event) => any) | null;
        onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
        onopen: ((this: WebSocket, ev: Event) => any) | null;
        OPEN: 1;
        protocol: string;
        readyState: 0 | 1 | 2 | 3;
        url: string;
        accept(options?: WebSocketAcceptOptions): void;
        addEventListener<K extends keyof WebSocketEventMap>(
            type: K,
            listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any,
            options?: boolean | AddEventListenerOptions,
        ): void;
        addEventListener(
            type: string,
            listener: EventListenerOrEventListenerObject,
            options?: boolean | AddEventListenerOptions,
        ): void;
        close(code?: number, reason?: string): void;
        close(code?: number, reason?: string): void;
        deserializeAttachment(): any;
        dispatchEvent(event: Event): boolean;
        removeEventListener<K extends keyof WebSocketEventMap>(
            type: K,
            listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any,
            options?: boolean | EventListenerOptions,
        ): void;
        removeEventListener(
            type: string,
            listener: EventListenerOrEventListenerObject,
            options?: boolean | EventListenerOptions,
        ): void;
        send(data: string | Blob | BufferSource): void;
        send(
            message: string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>,
        ): void;
        serializeAttachment(attachment: any): void;
    }

    Hierarchy

    • EventTarget
    • EventTarget<WebSocketEventMap>
    • _WebSocket
      • WebSocket
    Index

    Properties

    binaryType: BinaryType

    The WebSocket.binaryType property controls the type of binary data being received over the WebSocket connection.

    MDN Reference

    bufferedAmount: number

    The WebSocket.bufferedAmount read-only property returns 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.

    MDN Reference

    CLOSED: 3
    CLOSING: 2
    CONNECTING: 0
    extensions: string

    The WebSocket.extensions read-only property returns the extensions selected by the server. This is currently only the empty string or a list of extensions as negotiated by the connection.

    MDN Reference

    onclose: ((this: WebSocket, ev: CloseEvent) => any) | null
    onerror: ((this: WebSocket, ev: Event) => any) | null
    onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null
    onopen: ((this: WebSocket, ev: Event) => any) | null
    OPEN: 1
    protocol: string

    The WebSocket.protocol read-only property returns the name of the sub-protocol the server selected; this will be one of the strings specified in the protocols parameter when creating the WebSocket object, or the empty string if no connection is established.

    MDN Reference

    readyState: 0 | 1 | 2 | 3

    The WebSocket.readyState read-only property returns the current state of the WebSocket connection.

    MDN Reference

    url: string

    The WebSocket.url read-only property returns the absolute URL of the WebSocket as resolved by the constructor.

    MDN Reference

    Methods

    • The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

      MDN Reference

      Type Parameters

      • K extends keyof WebSocketEventMap

      Parameters

      • type: K
      • listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any
      • Optionaloptions: boolean | AddEventListenerOptions

      Returns void

    • The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

      MDN Reference

      Parameters

      • type: string
      • listener: EventListenerOrEventListenerObject
      • Optionaloptions: boolean | AddEventListenerOptions

      Returns void

    • The WebSocket.close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

      MDN Reference

      Parameters

      • Optionalcode: number
      • Optionalreason: string

      Returns void

    • The WebSocket.close() method closes the already CLOSED, this method does nothing.

      MDN Reference

      Parameters

      • Optionalcode: number
      • Optionalreason: string

      Returns void

    • Returns any

    • The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

      MDN Reference

      Parameters

      • event: Event

      Returns boolean

    • The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

      MDN Reference

      Type Parameters

      • K extends keyof WebSocketEventMap

      Parameters

      • type: K
      • listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any
      • Optionaloptions: boolean | EventListenerOptions

      Returns void

    • The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

      MDN Reference

      Parameters

      • type: string
      • listener: EventListenerOrEventListenerObject
      • Optionaloptions: boolean | EventListenerOptions

      Returns void

    • The WebSocket.send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data. If the data can't be sent (for example, because it needs to be buffered but the buffer is full), the socket is closed automatically. The browser will throw an exception if you call send() when the connection is in the CONNECTING state. If you call send() when the connection is in the CLOSING or CLOSED states, the browser will silently discard the data.

      MDN Reference

      Parameters

      • data: string | Blob | BufferSource

      Returns void

    • The WebSocket.send() method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of bufferedAmount by the number of bytes needed to contain the data.

      MDN Reference

      Parameters

      • message: string | ArrayBuffer | ArrayBufferView<ArrayBufferLike>

      Returns void

    • Parameters

      • attachment: any

      Returns void