Class WebSocketClient
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
urlstringThe URL to connect to.
stateChangedAction<ConnectionStates>Action to call when the connection state changes.
errorAction<Exception>Action to handle errors.
processFunc<WebSocketClient, WebSocketMessage, CancellationToken, ValueTask>Function to process incoming messages with reference to the client instance.
infoLogAction<string, object>Action to log informational messages.
errorLogAction<string, object>Action to log error messages.
verboseLogAction<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
urlstringThe URL to connect to.
stateChangedAction<ConnectionStates>Action to call when connection state changes.
errorAction<Exception>Action to handle errors.
processFunc<WebSocketMessage, CancellationToken, ValueTask>Function to process incoming messages.
infoLogAction<string, object>Action to log informational messages.
errorLogAction<string, object>Action to log error messages.
verboseLogAction<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
Properties
BufferSize
Gets or sets the buffer size for compressed data.
public int BufferSize { get; set; }
Property Value
BufferSizeUncompress
Gets or sets the buffer size for uncompressed data.
public int BufferSizeUncompress { get; set; }
Property Value
DisableAutoResend
Gets or sets a value that indicates whether auto resend is disabled.
public bool DisableAutoResend { get; set; }
Property Value
Encoding
Gets or sets the encoding used to convert between bytes and string data.
public Encoding Encoding { get; set; }
Property Value
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
IsConnected
Gets a value indicating whether the client is connected.
public bool IsConnected { get; }
Property Value
ReconnectAttempts
Gets or sets the number of reconnection attempts. -1 means infinite attempts, 0 means no reconnect.
public int ReconnectAttempts { get; set; }
Property Value
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
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
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
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
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
cancellationTokenCancellationTokenA 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
subIdlongThe subscription identifier.
ResendAsync(CancellationToken)
Asynchronously resends stored commands after reconnecting.
public ValueTask ResendAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenA 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
sendBufbyte[]The byte array to send.
typeWebSocketMessageTypeThe type of WebSocket message.
subIdlongThe subscription identifier.
preFunc<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
objobjectThe object to send. If not a byte array, it is converted to JSON.
subIdlongThe subscription identifier.
preFunc<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
sendBufbyte[]The byte array to send.
typeWebSocketMessageTypeThe type of WebSocket message.
subIdlongThe subscription identifier.
preFunc<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
sendBufbyte[]The byte array to send.
typeWebSocketMessageTypeThe type of WebSocket message.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
subIdlongThe subscription identifier.
preFunc<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
objobjectThe object to send. If not a byte array, it is converted to JSON.
subIdlongThe subscription identifier.
preFunc<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
objobjectThe object to send. If not a byte array, it is converted to JSON.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
subIdlongThe subscription identifier.
preFunc<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
codebyteThe 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
PostConnect
Occurs after a successful connection.
public event Func<bool, CancellationToken, ValueTask> PostConnect
Event Type
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