Table of Contents

Class WebSocketClient

Namespace
Ecng.Net
Assembly
Ecng.Net.SocketIO.dll

Represents a client for WebSocket connections.

public class WebSocketClient : Disposable, IDisposable, IConnection
Inheritance
WebSocketClient
Implements
Inherited Members
Extension Methods

Constructors

WebSocketClient(string, Action<ConnectionStates>, Action<Exception>, Func<WebSocketClient, WebSocketMessage, CancellationToken, ValueTask>, Action<string, object>, Action<string, object>, Action<string, object>)

Initializes a new instance of the WebSocketClient class.

public WebSocketClient(string url, Action<ConnectionStates> stateChanged, Action<Exception> error, Func<WebSocketClient, WebSocketMessage, CancellationToken, ValueTask> process, Action<string, object> infoLog, Action<string, object> errorLog, Action<string, object> verboseLog)

Parameters

url string

The URL to connect to.

stateChanged Action<ConnectionStates>

Action to call when the connection state changes.

error Action<Exception>

Action to handle errors.

process Func<WebSocketClient, WebSocketMessage, CancellationToken, ValueTask>

Function to process incoming messages with reference to the client instance.

infoLog Action<string, object>

Action to log informational messages.

errorLog Action<string, object>

Action to log error messages.

verboseLog Action<string, object>

Action to log verbose messages.

Exceptions

ArgumentNullException

If any required parameter is null.

WebSocketClient(string, Action<ConnectionStates>, Action<Exception>, Func<WebSocketMessage, CancellationToken, ValueTask>, Action<string, object>, Action<string, object>, Action<string, object>)

Initializes a new instance of the WebSocketClient class.

public WebSocketClient(string url, Action<ConnectionStates> stateChanged, Action<Exception> error, Func<WebSocketMessage, CancellationToken, ValueTask> process, Action<string, object> infoLog, Action<string, object> errorLog, Action<string, object> verboseLog)

Parameters

url string

The URL to connect to.

stateChanged Action<ConnectionStates>

Action to call when connection state changes.

error Action<Exception>

Action to handle errors.

process Func<WebSocketMessage, CancellationToken, ValueTask>

Function to process incoming messages.

infoLog Action<string, object>

Action to log informational messages.

errorLog Action<string, object>

Action to log error messages.

verboseLog Action<string, object>

Action to log verbose messages.

Exceptions

ArgumentNullException

If any required parameter is null.

Fields

DisconnectTimeout

Gets or sets the disconnect timeout period.

public TimeSpan DisconnectTimeout

Field Value

TimeSpan

Properties

BufferSize

Gets or sets the buffer size for compressed data.

public int BufferSize { get; set; }

Property Value

int

BufferSizeUncompress

Gets or sets the buffer size for uncompressed data.

public int BufferSizeUncompress { get; set; }

Property Value

int

DisableAutoResend

Gets or sets a value that indicates whether auto resend is disabled.

public bool DisableAutoResend { get; set; }

Property Value

bool

Encoding

Gets or sets the encoding used to convert between bytes and string data.

public Encoding Encoding { get; set; }

Property Value

Encoding

Exceptions

ArgumentNullException

Thrown if a null value is set.

Indent

Gets or sets a value that indicates whether output JSON should be indented.

public bool Indent { get; set; }

Property Value

bool

IsConnected

Gets a value indicating whether the client is connected.

public bool IsConnected { get; }

Property Value

bool

ReconnectAttempts

Gets or sets the number of reconnection attempts. -1 means infinite attempts, 0 means no reconnect.

public int ReconnectAttempts { get; set; }

Property Value

int

Exceptions

ArgumentOutOfRangeException

Thrown if value is less than -1.

ReconnectInterval

Gets or sets the interval between reconnection attempts.

public TimeSpan ReconnectInterval { get; set; }

Property Value

TimeSpan

Exceptions

ArgumentOutOfRangeException

Thrown if a negative time span is set.

ResendInterval

Gets or sets the interval between resend attempts for messages.

public TimeSpan ResendInterval { get; set; }

Property Value

TimeSpan

Exceptions

ArgumentOutOfRangeException

Thrown if a negative time span is set.

ResendTimeout

Gets or sets the timeout before resend of commands after reconnection.

public TimeSpan ResendTimeout { get; set; }

Property Value

TimeSpan

SendSettings

Gets or sets the JSON serializer settings used when sending objects.

public JsonSerializerSettings SendSettings { get; set; }

Property Value

JsonSerializerSettings

State

Gets the current connection state.

public ConnectionStates State { get; }

Property Value

ConnectionStates

Methods

Connect()

Connects to the server synchronously.

public void Connect()

Remarks

This method runs the asynchronous connection method synchronously.

ConnectAsync(CancellationToken)

Asynchronously connects to the server.

public ValueTask ConnectAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

ValueTask

A task that represents the asynchronous connect operation.

Disconnect()

Disconnects from the server.

public void Disconnect()

Exceptions

InvalidOperationException

Thrown if the client is not connected.

DisposeManaged()

Disposes the managed resources. Override this method to add custom clean up of managed resources.

protected override void DisposeManaged()

RemoveResend()

Removes all resend commands.

public void RemoveResend()

RemoveResend(long)

Removes a resend command for the specified subscription identifier.

public void RemoveResend(long subId)

Parameters

subId long

The subscription identifier.

ResendAsync(CancellationToken)

Asynchronously resends stored commands after reconnecting.

public ValueTask ResendAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

ValueTask

A task that represents the asynchronous resend operation.

Send(byte[], WebSocketMessageType, long, Func<long, CancellationToken, ValueTask>)

Sends a byte array message to the server synchronously.

public void Send(byte[] sendBuf, WebSocketMessageType type, long subId = 0, Func<long, CancellationToken, ValueTask> pre = null)

Parameters

sendBuf byte[]

The byte array to send.

type WebSocketMessageType

The type of WebSocket message.

subId long

The subscription identifier.

pre Func<long, CancellationToken, ValueTask>

A pre-send callback function.

Send(object, long, Func<long, CancellationToken, ValueTask>)

Sends an object to the server synchronously.

public void Send(object obj, long subId = 0, Func<long, CancellationToken, ValueTask> pre = null)

Parameters

obj object

The object to send. If not a byte array, it is converted to JSON.

subId long

The subscription identifier.

pre Func<long, CancellationToken, ValueTask>

A pre-send callback function.

SendAsync(byte[], WebSocketMessageType, long, Func<long, CancellationToken, ValueTask>)

Asynchronously sends a byte array message to the server.

public ValueTask SendAsync(byte[] sendBuf, WebSocketMessageType type, long subId = 0, Func<long, CancellationToken, ValueTask> pre = null)

Parameters

sendBuf byte[]

The byte array to send.

type WebSocketMessageType

The type of WebSocket message.

subId long

The subscription identifier.

pre Func<long, CancellationToken, ValueTask>

A pre-send callback function.

Returns

ValueTask

A task that represents the asynchronous send operation.

SendAsync(byte[], WebSocketMessageType, CancellationToken, long, Func<long, CancellationToken, ValueTask>)

Asynchronously sends a byte array message to the server.

public ValueTask SendAsync(byte[] sendBuf, WebSocketMessageType type, CancellationToken cancellationToken, long subId = 0, Func<long, CancellationToken, ValueTask> pre = null)

Parameters

sendBuf byte[]

The byte array to send.

type WebSocketMessageType

The type of WebSocket message.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

subId long

The subscription identifier.

pre Func<long, CancellationToken, ValueTask>

A pre-send callback function.

Returns

ValueTask

A task that represents the asynchronous send operation.

SendAsync(object, long, Func<long, CancellationToken, ValueTask>)

Asynchronously sends an object to the server.

public ValueTask SendAsync(object obj, long subId = 0, Func<long, CancellationToken, ValueTask> pre = null)

Parameters

obj object

The object to send. If not a byte array, it is converted to JSON.

subId long

The subscription identifier.

pre Func<long, CancellationToken, ValueTask>

A pre-send callback function.

Returns

ValueTask

A task that represents the asynchronous send operation.

SendAsync(object, CancellationToken, long, Func<long, CancellationToken, ValueTask>)

Asynchronously sends an object to the server.

public ValueTask SendAsync(object obj, CancellationToken cancellationToken, long subId = 0, Func<long, CancellationToken, ValueTask> pre = null)

Parameters

obj object

The object to send. If not a byte array, it is converted to JSON.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

subId long

The subscription identifier.

pre Func<long, CancellationToken, ValueTask>

A pre-send callback function.

Returns

ValueTask

A task that represents the asynchronous send operation.

SendOpCode(byte)

Sends an operation code directly.

public ValueTask SendOpCode(byte code = 9)

Parameters

code byte

The operation code to send (default is 0x9 for ping).

Returns

ValueTask

A task that represents the asynchronous operation.

Events

Init

Occurs when the client web socket is initialized.

public event Action<ClientWebSocket> Init

Event Type

Action<ClientWebSocket>

PostConnect

Occurs after a successful connection.

public event Func<bool, CancellationToken, ValueTask> PostConnect

Event Type

Func<bool, CancellationToken, ValueTask>

PreProcess

Occurs before processing received data.

public event Func<ArraySegment<byte>, byte[], int> PreProcess

Event Type

Func<ArraySegment<byte>, byte[], int>

StateChanged

Occurs when the connection state changes.

public event Action<ConnectionStates> StateChanged

Event Type

Action<ConnectionStates>