Table of Contents

Class WebSocket

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

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.

[Value("WebSocket")]
public class WebSocket : EventTarget
Inheritance
WebSocket
Inherited Members

Remarks

To construct a WebSocket, use the WebSocket() constructor.

NOTE

The WebSocket API has no way to apply backpressure, therefore when messages arrive faster than the application can process them, the application will either fill up the device's memory by buffering those messages, become unresponsive due to 100% CPU usage, or both. For an alternative that provides backpressure automatically, see WebSocketStream.

-Writing WebSocket client applications

See also on MDN

Constructors

WebSocket()

public WebSocket()

WebSocket(string, Union232)

The WebSocket() constructor returns a new WebSocket object and immediately attempts to establish a connection to the specified WebSocket URL.

public WebSocket(string url, Union232 protocols = default)

Parameters

url string
protocols Union232

Remarks

-RFC 6455 (the WebSocket Protocol specification)

See also on MDN

Fields

CLOSED

[Value("CLOSED")]
public const ushort CLOSED = 3

Field Value

ushort

CLOSING

[Value("CLOSING")]
public const ushort CLOSING = 2

Field Value

ushort

CONNECTING

[Value("CONNECTING")]
public const ushort CONNECTING = 0

Field Value

ushort

OPEN

[Value("OPEN")]
public const ushort OPEN = 1

Field Value

ushort

Properties

BinaryType

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

[Value("binaryType")]
public BinaryType BinaryType { get; set; }

Property Value

BinaryType

A string:

Remarks

BufferedAmount

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.

[Value("bufferedAmount")]
public ulong BufferedAmount { get; }

Property Value

ulong

An unsigned long.

Remarks

Extensions

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.

[Value("extensions")]
public string Extensions { get; }

Property Value

string

A string.

Remarks

Onclose

[Value("onclose")]
public EventHandlerNonNull Onclose { get; set; }

Property Value

EventHandlerNonNull

Onerror

[Value("onerror")]
public EventHandlerNonNull Onerror { get; set; }

Property Value

EventHandlerNonNull

Onmessage

[Value("onmessage")]
public EventHandlerNonNull Onmessage { get; set; }

Property Value

EventHandlerNonNull

Onopen

[Value("onopen")]
public EventHandlerNonNull Onopen { get; set; }

Property Value

EventHandlerNonNull

Protocol

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.

[Value("protocol")]
public string Protocol { get; }

Property Value

string

A string.

Remarks

ReadyState

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

[Value("readyState")]
public ushort ReadyState { get; }

Property Value

ushort

A number which is one of the four possible state constants defined on the WebSocket interface:

Remarks

Url

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

[Value("url")]
public string Url { get; }

Property Value

string

A string.

Remarks

Methods

Close(ushort, string)

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

[Value("close")]
public GlobalObject.Undefined Close(ushort code = 0, string reason = null)

Parameters

code ushort
reason string

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

NOTE

The process of closing the connection begins with a closing handshake, and the close() method does not discard previously-sent messages before starting that closing handshake; even if the user agent is still busy sending those messages, the handshake will only start after the messages are sent.

-RFC 6455 (the WebSocket Protocol specification)

See also on MDN

Send(Union233)

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.

[Value("send")]
public GlobalObject.Undefined Send(Union233 data)

Parameters

data Union233

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks