WebSocket Class

A WebSocket connection (https://websockets.spec.whatwg.org/#the-websocket-interface): a two-way communication with a server, established by an opening handshake that is fetched as specified (cookies, Origin, mixed content, bad ports...), whose connection is opened by DocumentLoader.ConnectWebSocketAsync. The frames of the WebSocket protocol (https://www.rfc-editor.org/rfc/rfc6455) are read and written by WebSocket, with the permessage-deflate extension when the server accepts it.

public sealed class WebSocket : EventTarget

Remarks

The relevant global object of the WebSocket is the current global object when it is created (the window or the worker of the script that creates it): its events are fired by tasks of its event loop. A WebSocket created without current global object cannot connect: its WebSocket.ReadyState is WebSocket.Closed.

A WebSocket that is connecting or closing is an asynchronous operation of its event loop (see DomEventLoop.RunUntilIdleAsync), as are the messages that are being sent. An open connection that waits for messages is not: DomEventLoop.RunUntilIdleAsync does not wait for the next message. The WebSockets of a document are closed when the document is unloaded, with the 1001 (going away) status code, and so are the WebSockets of a worker when it ends.

See also

Inheritance
EventTarget WebSocket

Constructors

NameDescription
WebSocket(string, object?)Creates a WebSocket and starts to establish its connection (https://websockets.spec.whatwg.org/#dom-websocket-websocket).

WebSocket(string, object?)

WebSocket.cs

Creates a WebSocket and starts to establish its connection (https://websockets.spec.whatwg.org/#dom-websocket-websocket).

public WebSocket(string url, object? protocols = null)

Parameters

url string
The URL of the server: a ws: or wss: URL (an http: or https: URL is converted), possibly relative to the API base URL of the relevant global object, without fragment.
protocols object?
The subprotocols the server can select: null (none), a string, or a sequence of strings (e.g. a String array). The connection fails when the server does not select one of them.

Exceptions

DomException
A SyntaxError when the URL is not valid, has another scheme or a fragment, or when a subprotocol is not a valid token or appears twice (ASCII case-insensitively).

See also

Properties

NameDescription
BinaryTypeGets or sets how the binary messages are exposed: as a Blob (the default) or as a Byte array (an ArrayBuffer).
BufferedAmountGets the number of bytes of the messages queued by WebSocket.Send that were not yet transmitted to the network as of the last task (https://websockets.spec.whatwg.org/#dom-websocket-bufferedamount). It only increases once the connection is closed.
ExtensionsGets the extensions selected by the server (the Sec-WebSocket-Extensions header of its response), or the empty string.
OnCloseGets or sets the event handler of the close event.
OnErrorGets or sets the event handler of the error event.
OnMessageGets or sets the event handler of the message event.
OnOpenGets or sets the event handler of the open event.
ProtocolGets the subprotocol selected by the server (the Sec-WebSocket-Protocol header of its response), or the empty string.
ReadyStateGets the state of the connection (https://websockets.spec.whatwg.org/#dom-websocket-readystate): WebSocket.Connecting, WebSocket.Open, WebSocket.Closing or WebSocket.Closed.
UrlGets the URL of the WebSocket, serialized (https://websockets.spec.whatwg.org/#dom-websocket-url).

BinaryType

WebSocket.cs

Gets or sets how the binary messages are exposed: as a Blob (the default) or as a Byte array (an ArrayBuffer).

public BinaryType BinaryType { get; set; }

BufferedAmount

WebSocket.cs

Gets the number of bytes of the messages queued by WebSocket.Send that were not yet transmitted to the network as of the last task (https://websockets.spec.whatwg.org/#dom-websocket-bufferedamount). It only increases once the connection is closed.

public ulong BufferedAmount { get; }

Extensions

WebSocket.cs

Gets the extensions selected by the server (the Sec-WebSocket-Extensions header of its response), or the empty string.

public string Extensions { get; }

Gets or sets the event handler of the close event.

public Action<Event>? OnClose { get; set; }

Gets or sets the event handler of the error event.

public Action<Event>? OnError { get; set; }

OnMessage

WebSocket.cs

Gets or sets the event handler of the message event.

public Action<Event>? OnMessage { get; set; }

Gets or sets the event handler of the open event.

public Action<Event>? OnOpen { get; set; }

Protocol

WebSocket.cs

Gets the subprotocol selected by the server (the Sec-WebSocket-Protocol header of its response), or the empty string.

public string Protocol { get; }

ReadyState

WebSocket.cs

Gets the state of the connection (https://websockets.spec.whatwg.org/#dom-websocket-readystate): WebSocket.Connecting, WebSocket.Open, WebSocket.Closing or WebSocket.Closed.

public ushort ReadyState { get; }

Gets the URL of the WebSocket, serialized (https://websockets.spec.whatwg.org/#dom-websocket-url).

public string Url { get; }

Methods

NameDescription
Close(ushort?, string?)Closes the connection (https://websockets.spec.whatwg.org/#dom-websocket-close): starts the closing handshake, or fails the connection when it is not established yet.
Send(object?)Sends a message (https://websockets.spec.whatwg.org/#dom-websocket-send): a text message for a string, a binary message for a Blob or bytes (a Byte array, a ReadOnlyMemory<T> of bytes, or a buffer source of the script engine). The message is not sent when the closing handshake was started, but WebSocket.BufferedAmount increases anyway.
Send(string)Sends a text message (https://websockets.spec.whatwg.org/#dom-websocket-send).

Close(ushort?, string?)

WebSocket.cs

Closes the connection (https://websockets.spec.whatwg.org/#dom-websocket-close): starts the closing handshake, or fails the connection when it is not established yet.

public void Close(ushort? code = null, string? reason = null)

Parameters

code ushort?
The status code of the Close frame: 1000 or a code from 3000 to 4999, or null for a Close frame without status code.
reason string?
The reason of the Close frame (at most 123 bytes in UTF-8), or null.

Exceptions

DomException
An InvalidAccessError when the code is not valid, or a SyntaxError when the reason is too long.

See also

Send overloads

Send(object?)

WebSocket.cs

Sends a message (https://websockets.spec.whatwg.org/#dom-websocket-send): a text message for a string, a binary message for a Blob or bytes (a Byte array, a ReadOnlyMemory<T> of bytes, or a buffer source of the script engine). The message is not sent when the closing handshake was started, but WebSocket.BufferedAmount increases anyway.

public void Send(object? data)
Parameters
data object?
The message.
Exceptions
DomException
An InvalidStateError when the connection is not established yet.
See also

Send(string)

WebSocket.cs

Sends a text message (https://websockets.spec.whatwg.org/#dom-websocket-send).

public void Send(string data)
Parameters
data string
The text of the message.

Constants

NameDescription
ClosedThe state of a WebSocket whose connection is closed or could not be opened.
ClosingThe state of a WebSocket whose closing handshake was started, or whose WebSocket.Close method was called.
ConnectingThe state of a WebSocket whose connection has not yet been established.
OpenThe state of a WebSocket whose connection is established: the messages can be sent and received.

Closed

The state of a WebSocket whose connection is closed or could not be opened.

public const ushort Closed = 3

Closing

The state of a WebSocket whose closing handshake was started, or whose WebSocket.Close method was called.

public const ushort Closing = 2

Connecting

The state of a WebSocket whose connection has not yet been established.

public const ushort Connecting = 0

Open

The state of a WebSocket whose connection is established: the messages can be sent and received.

public const ushort Open = 1