Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Net.​WebSockets.​WebSocket
Assembly: System.Net.WebSockets
Inheritance: object → WebSocket
Implemented Interfaces
The WebSocket class allows applications to send and receive data after the WebSocket upgrade has completed.
Properties
public
WebSocketCloseStatus?
CloseStatus
Indicates the reason why the remote endpoint initiated the close handshake.
public
string
CloseStatusDescription
Allows the remote endpoint to describe the reason why the connection was closed.
public static
TimeSpan
DefaultKeepAliveInterval
Gets the default WebSocket protocol keep-alive interval.
public
WebSocketState
State
Returns the current state of the WebSocket connection.
public
string
SubProtocol
Gets the subprotocol that was negotiated during the opening handshake.
Methods
public
void
Abort​()
Aborts the WebSocket connection and cancels any pending IO operations.
CloseAsync​(WebSocketCloseStatus closeStatus,
string statusDescription,
Threading.​CancellationToken cancellationToken)
Closes the WebSocket connection as an asynchronous operation using the close handshake defined in the WebSocket protocol specification section 7.
Returns The task object representing the asynchronous operation.
closeStatus
Indicates the reason for closing the WebSocket connection.
statusDescription
Specifies a human readable explanation as to why the connection is closed.
cancellationToken
The token that can be used to propagate notification that operations should be canceled.
CloseOutputAsync​(WebSocketCloseStatus closeStatus,
string statusDescription,
Threading.​CancellationToken cancellationToken)
Initiates or completes the close handshake defined in the WebSocket protocol specification section 7.
Returns The task object representing the asynchronous operation.
closeStatus
Indicates the reason for closing the WebSocket connection.
statusDescription
Allows applications to specify a human readable explanation as to why the connection is closed.
cancellationToken
The token that can be used to propagate notification that operations should be canceled.
public static
ArraySegment<​byte>
CreateClientBuffer​(int receiveBufferSize,
int sendBufferSize)
Create client buffers to use with this <see cref="T:System.Net.WebSockets.WebSocket" /> instance.
Returns An array with the client buffers.
receiveBufferSize
The size, in bytes, of the client receive buffer.
sendBufferSize
The size, in bytes, of the send buffer.
public static
WebSocket
CreateClientWebSocket​(IO.​Stream innerStream,
string subProtocol,
int receiveBufferSize,
int sendBufferSize,
TimeSpan keepAliveInterval,
bool useZeroMaskingKey,
ArraySegment<​byte> internalBuffer)
Allows callers to create a client side WebSocket class which will use the WSPC for framing purposes.
Returns Returns <see cref="T:System.Net.WebSockets.WebSocket" /> .
innerStream
The connection to be used for IO operations.
subProtocol
The subprotocol accepted by the client.
receiveBufferSize
The size in bytes of the client WebSocket receive buffer.
sendBufferSize
The size in bytes of the client WebSocket send buffer.
keepAliveInterval
Determines how regularly a frame is sent over the connection as a keep-alive. Applies only when the connection is idle.
useZeroMaskingKey
Indicates whether a random key or a static key (just zeros) should be used for the WebSocket masking.
internalBuffer
Will be used as the internal buffer in the WPC. The size has to be at least <c>2 * ReceiveBufferSize + SendBufferSize + 256 + 20 (16 on 32-bit)</c> .
public static
WebSocket
CreateFromStream​(IO.​Stream stream,
bool isServer,
string subProtocol,
TimeSpan keepAliveInterval)
Creates a new <see cref="T:System.Net.WebSockets.WebSocket" /> object that operates on the specified stream, which represents a web socket connection.
Returns The new web socket.
stream
The stream for the connection.
isServer
<see langword="true" /> to indicate it's the server-side of the connection; <see langword="false" /> if it's the client-side.
subProtocol
The agreed upon sub-protocol that was used when creating the connection.
keepAliveInterval
The keep-alive interval to use, or <see cref="F:System.Threading.Timeout.InfiniteTimeSpan" /> to disable keep-alives.
public static
WebSocket
CreateFromStream​(IO.​Stream stream,
WebSocketCreationOptions options)
Creates a <see cref="T:System.Net.WebSockets.WebSocket" /> that operates on a <see cref="T:System.IO.Stream" /> representing a web socket connection.
Returns The created web socket.
stream
The <see cref="T:System.IO.Stream" /> for the connection.
options
The options with which the websocket must be created.
public static
ArraySegment<​byte>
CreateServerBuffer​(int receiveBufferSize)
Creates a WebSocket server buffer.
Returns Returns <see cref="T:System.ArraySegment`1" /> .
receiveBufferSize
The size, in bytes, of the desired buffer.
public
void
Dispose​()
Used to clean up unmanaged resources for ASP.NET and self-hosted implementations.
public static
bool
IsApplicationTargeting45​()
Returns a value that indicates if the WebSocket instance is targeting .NET Framework 4.5.
Returns <see langword="true" /> if the <see cref="T:System.Net.WebSockets.WebSocket" /> is targeting .NET Framework 4.5; otherwise, <see langword="false" /> .
protected static
bool
IsStateTerminal​(WebSocketState state)
Returns a value that indicates if the state of the WebSocket instance is closed or aborted.
Returns <see langword="true" /> if the <see cref="T:System.Net.WebSockets.WebSocket" /> is closed or aborted; otherwise, <see langword="false" /> .
state
The current state of the WebSocket.
public
Threading.​Tasks.​Task<​WebSocketReceiveResult>
ReceiveAsync​(ArraySegment<​byte> buffer,
Threading.​CancellationToken cancellationToken)
Receives data from the <see cref="T:System.Net.WebSockets.WebSocket" /> connection asynchronously.
Returns The task object representing the asynchronous operation. The <see cref="P:System.Threading.Tasks.Task`1.Result" /> property on the task object returns a <see cref="T:System.Net.WebSockets.WebSocketReceiveResult" /> object that represents the received data.
buffer
References the application buffer that is the storage location for the received data.
cancellationToken
Propagates the notification that operations should be canceled.
public
Threading.​Tasks.​ValueTask<​ValueWebSocketReceiveResult>
ReceiveAsync​(Memory<​byte> buffer,
Threading.​CancellationToken cancellationToken)
Receives data from the <see cref="T:System.Net.WebSockets.WebSocket" /> connection asynchronously.
Returns The task object representing the asynchronous operation. The <see cref="P:System.Threading.Tasks.ValueTask`1.Result" /> property on the task object returns a <see cref="T:System.Net.WebSockets.ValueWebSocketReceiveResult" /> object that represents the received data.
buffer
The application buffer that is the storage location for the received data.
cancellationToken
The cancellation token to use to cancel the receive operation.
public static
void
RegisterPrefixes​()
Allows callers to register prefixes for WebSocket requests (ws and wss).
SendAsync​(ArraySegment<​byte> buffer,
WebSocketMessageType messageType,
bool endOfMessage,
Threading.​CancellationToken cancellationToken)
Sends data over the <see cref="T:System.Net.WebSockets.WebSocket" /> connection asynchronously.
Returns The task object representing the asynchronous operation.
buffer
The buffer to be sent over the connection.
messageType
Indicates whether the application is sending a binary or text message.
endOfMessage
Indicates whether the data in "buffer" is the last part of a message.
cancellationToken
The token that propagates the notification that operations should be canceled.
SendAsync​(ReadOnlyMemory<​byte> buffer,
WebSocketMessageType messageType,
bool endOfMessage,
Threading.​CancellationToken cancellationToken)
Sends data over the <see cref="T:System.Net.WebSockets.WebSocket" /> connection asynchronously.
Returns The task object representing the asynchronous operation.
buffer
The buffer to be sent over the connection.
messageType
One of the enumeration values that indicates whether the application is sending a binary or text message.
endOfMessage
<see langword="true" /> to indicate the data in "buffer" is the last part of a message; otherwise, <see langword="false" /> .
cancellationToken
The cancellation token to use to cancel the send operation.
SendAsync​(ReadOnlyMemory<​byte> buffer,
WebSocketMessageType messageType,
WebSocketMessageFlags messageFlags,
Threading.​CancellationToken cancellationToken)
Sends data over the <see cref="T:System.Net.WebSockets.WebSocket" /> connection asynchronously.
Returns The task object representing the asynchronous operation.
buffer
The buffer to be sent over the connection.
messageType
One of the enumeration values that indicates whether the application is sending a binary or text message.
messageFlags
Flags for controlling how the <see cref="T:System.Net.WebSockets.WebSocket" /> should send a message.
cancellationToken
The cancellation token to use to cancel the send operation.
protected static
void
ThrowOnInvalidState​(WebSocketState state,
WebSocketState[] validStates)
Verifies that the connection is in an expected state.
state
The current state of the WebSocket to be tested against the list of valid states.
validStates
List of valid connection states.
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object