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
WebSocketOptionsOptional WebSocketOptions where extra options can be defined.
Properties
IsServer
public bool IsServer { get; }
Property Value
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
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).
public int MaxFragmentSize { get; }
Property Value
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
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
WebSocketCloseStatusIndicates the reason for closing the WebSocket connection.
statusDescription
stringSpecifies 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
NetworkStreamThe stream for the connection.
isServer
booltrue to indicate it's the server-side of the connection; false if it's the client-side.
socket
SocketThe 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
WebSocketMessageTypeIndicates whether the application is sending a binary or text message.
fragmentSize
intIndicates whether the application is sending a binary or text message. Default -1 will use the maxFragmentSize
Returns
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
intIndicates whether the application is sending a binary or text message. Default -1 will use the maxFragmentSize
Returns
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
stringThe text that will be send
fragmentSize
intIndicates whether the application is sending a binary or text message. Default -1 will use the maxFragmentSize
Returns
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
MessageReceived
Event raised when a message is received by the WebSocket.
public event WebSocket.MessageReceivedEventHandler MessageReceived