Table of Contents

Class WebSocket

Namespace
System.Net.WebSockets
Assembly
System.Net.WebSockets.dll

The WebSocket class allows applications to send and receive data after the WebSocket upgrade has completed.

public abstract class WebSocket : IDisposable
Inheritance
WebSocket
Implements
Derived
Inherited Members
Extension Methods

Constructors

WebSocket(WebSocketOptions)

Creates an instance of the System.Net.WebSockets.WebSocket class.

protected WebSocket(WebSocketOptions options = null)

Parameters

options WebSocketOptions

Optional WebSocketOptions where extra options can be defined.

Properties

IsServer

true to indicate it's the server-side of the connection; false if it's the client-side.

public bool IsServer { get; }

Property Value

bool

KeepAliveInterval

The interval that the client will send keep alive messages to let the server know to not close the connection. Default is 30 second interval.

public TimeSpan KeepAliveInterval { get; }

Property Value

TimeSpan

MaxFragmentSize

Gets or sets the maximum allowed byte length of a partial message send by the WebSocket. By default if a message that exceeds the size limit it will be broken up in smaller partial messages Default is 32768 bytes (32kB). The maximum allowed byte length of a partial message send by the WebSocket.

public int MaxFragmentSize { get; }

Property Value

int

MaxReceiveFrameSize

Gets the maximum allowed byte length of messages received by the WebSocket .

public int MaxReceiveFrameSize { get; }

Property Value

int

The maximum allowed byte length of messages received by the WebSocket. Default is int.MaxValue.

RemoteEndPoint

Gets the Remote Endpoint where the WebSocket connects to.

public IPEndPoint RemoteEndPoint { get; }

Property Value

IPEndPoint

The Remote Endpoint where the WebSocket connects to.

ServerTimeout

The timeout which specifies how long to wait for a message before closing the connection. Default is 60 seconds.

public TimeSpan ServerTimeout { get; }

Property Value

TimeSpan

State

Gets the WebSocket state of the System.Net.WebSockets.ClientWebSocket instance.

public abstract WebSocketState State { get; set; }

Property Value

WebSocketState

The WebSocket state of the System.Net.WebSockets.ClientWebSocket instance.

Methods

Abort()

Aborts the WebSocket connection and cancels any pending IO operations.

public void Abort()

Close(WebSocketCloseStatus, string)

Will start closing the WebSocket connection using the close handshake defined in the WebSocket protocol specification section 7.

public void Close(WebSocketCloseStatus closeStatus = WebSocketCloseStatus.Empty, string statusDescription = null)

Parameters

closeStatus WebSocketCloseStatus

Indicates the reason for closing the WebSocket connection.

statusDescription string

Specifies a human readable explanation as to why the connection is closed.

Remarks

WebSocketCloseStatus.EndpointUnavailable will close the WebSocket synchronous without awaiting response.

ConnectToStream(NetworkStream, bool, Socket)

Connects the WebSocket to the specified stream, which represents a web socket connection.

protected void ConnectToStream(NetworkStream stream, bool isServer, Socket socket)

Parameters

stream NetworkStream

The stream for the connection.

isServer bool

true to indicate it's the server-side of the connection; false if it's the client-side.

socket Socket

The socket on which the WebSocket connects to.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Send(byte[], WebSocketMessageType, int)

Sends data over the System.Net.WebSockets.WebSocket connection.

public bool Send(byte[] buffer, WebSocketMessageType messageType, int fragmentSize = -1)

Parameters

buffer byte[]

The buffer containing the message content.

messageType WebSocketMessageType

Indicates whether the application is sending a binary or text message.

fragmentSize int

Indicates whether the application is sending a binary or text message. Default -1 will use the maxFragmentSize

Returns

bool

true if the message was successfully queued for send. false otherwise.

Remarks

Messages are buffered send synchronously using a single send thread. The send message is not awaited.

SendBytes(byte[], int)

Sends a binary message over the System.Net.WebSockets.WebSocket connection.

public bool SendBytes(byte[] data, int fragmentSize = -1)

Parameters

data byte[]

The binary data that will be send.

fragmentSize int

Indicates whether the application is sending a binary or text message. Default -1 will use the maxFragmentSize

Returns

bool

true if the message was successfully queued for send. false otherwise.

Remarks

Messages are buffered send synchronously using a single send thread. The send message is not awaited.

SendString(string, int)

Sends a text message over the System.Net.WebSockets.WebSocket connection.

public bool SendString(string message, int fragmentSize = -1)

Parameters

message string

The text that will be send

fragmentSize int

Indicates whether the application is sending a binary or text message. Default -1 will use the maxFragmentSize

Returns

bool

true if the message was successfully queued for send. false otherwise.

Remarks

Messages are buffered send synchronously using a single send thread. The send message is not awaited.

Events

ConnectionClosed

Occurs when the connection is closed. The connection could be closed due to an error or due to either the server or client intentionally closing the connection without error.

public event EventHandler ConnectionClosed

Event Type

EventHandler

MessageReceived

Event raised when a message is received by the WebSocket.

public event WebSocket.MessageReceivedEventHandler MessageReceived

Event Type

WebSocket.MessageReceivedEventHandler