class
WebSocket
public sealed class WebSocket A WebSocket client for connecting to external services.
Remarks
Events handlers will be called on the synchronization context that Connect was called on.
Constructors
WebSocket(Int32 maxMessageSize) Initialized a new WebSocket client.
maxMessageSize — The maximum message size to allow from the server, in bytes. Default 64 KiB. Properties
| Name | Type | Description |
|---|---|---|
IsConnected | Boolean | Returns true as long as a WebSocket connection is established. |
SubProtocol | String | Get the sub-protocol that was negotiated during the opening handshake. |
EnableCompression | Boolean | Enable or disable compression for the websocket. If the server supports it, compression will be enabled for all messages. Note: compression is disabled by default, and can be dangerous if you are sending secrets across the network. |
Methods
virtual Void Dispose() Cleans up resources used by the WebSocket client. This will also immediately close the connection if it is currently open.
Void AddSubProtocol(String protocol) Add a sub-protocol to be negotiated during the WebSocket connection handshake.
protocol Task Connect(String websocketUri, CancellationToken ct = null) Establishes a connection to an external WebSocket service.
websocketUri — The WebSocket URI to connect to. For example, "ws://hostname.local:1280/" for unencrypted WebSocket or "wss://hostname.local:1281/" for encrypted. ct — A CancellationToken which allows the connection attempt to be aborted if necessary. returns — A Task which completes when the connection is established, or throws if it failed to connect.
Task Connect(String websocketUri, Dictionary<String, String> headers, CancellationToken ct = null) ValueTask Send(String message) Sends a text message to the WebSocket server.
message — The message text to send. Must not be null. returns — A ValueTask which completes when the message was queued to be sent.
ValueTask Send(Byte[] data) Sends a binary message to the WebSocket server.
data — The message data to send. Must not be null. returns — A ValueTask which completes when the message was queued to be sent.
ValueTask Send(ArraySegment<Byte> data) ValueTask Send(Span<Byte> data)