WebSocketClient

Ecng.Net

Represents a client for WebSocket connections.

Herda de: Disposable

Implementa: IConnection

Construtores

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

Initializes a new instance of the WebSocketClient class.

url
The URL to connect to.
stateChanged
Async callback when the connection state changes.
error
Async callback to handle errors.
process
Function to process incoming messages with reference to the client instance.
infoLog
Action to log informational messages.
errorLog
Action to log error messages.
verboseLog
Action to log verbose messages.
WebSocketClient
public WebSocketClient(string url, Func<ConnectionStates, CancellationToken, ValueTask> stateChanged, Func<Exception, CancellationToken, ValueTask> error, Func<WebSocketMessage, CancellationToken, ValueTask> process, Action<string, object> infoLog, Action<string, object> errorLog, Action<string, object> verboseLog)
webSocketClient = WebSocketClient(url, stateChanged, error, process, infoLog, errorLog, verboseLog)

Initializes a new instance of the WebSocketClient class.

url
The URL to connect to.
stateChanged
Async callback when connection state changes.
error
Async callback to handle errors.
process
Function to process incoming messages.
infoLog
Action to log informational messages.
errorLog
Action to log error messages.
verboseLog
Action to log verbose messages.

Propriedades

BufferSize
public int BufferSize { get; set; }
value = webSocketClient.BufferSize
webSocketClient.BufferSize = value

Gets or sets the buffer size for compressed data.

BufferSizeUncompress
public int BufferSizeUncompress { get; set; }
value = webSocketClient.BufferSizeUncompress
webSocketClient.BufferSizeUncompress = value

Gets or sets the buffer size for uncompressed data.

DisableAutoResend
public bool DisableAutoResend { get; set; }
value = webSocketClient.DisableAutoResend
webSocketClient.DisableAutoResend = value

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

Encoding
public Encoding Encoding { get; set; }
value = webSocketClient.Encoding
webSocketClient.Encoding = value

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

Indent
public bool Indent { get; set; }
value = webSocketClient.Indent
webSocketClient.Indent = value

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

IsConnected
public bool IsConnected { get; }
value = webSocketClient.IsConnected

Gets a value indicating whether the client is connected.

ReconnectAttempts
public int ReconnectAttempts { get; set; }
value = webSocketClient.ReconnectAttempts
webSocketClient.ReconnectAttempts = value

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

ReconnectInterval
public TimeSpan ReconnectInterval { get; set; }
value = webSocketClient.ReconnectInterval
webSocketClient.ReconnectInterval = value

Gets or sets the interval between reconnection attempts.

ResendInterval
public TimeSpan ResendInterval { get; set; }
value = webSocketClient.ResendInterval
webSocketClient.ResendInterval = value

Gets or sets the interval between resend attempts for messages.

ResendTimeout
public TimeSpan ResendTimeout { get; set; }
value = webSocketClient.ResendTimeout
webSocketClient.ResendTimeout = value

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

SendSettings
public JsonSerializerSettings SendSettings { get; set; }
value = webSocketClient.SendSettings
webSocketClient.SendSettings = value

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

State
public ConnectionStates State { get; private set; }
value = webSocketClient.State
webSocketClient.State = value

Gets the current connection state.

WorkingTime
public WorkingTime WorkingTime { get; set; }
value = webSocketClient.WorkingTime
webSocketClient.WorkingTime = value

Gets or sets the working time schedule. When enabled, reconnection attempts are suppressed outside working hours.

Métodos

Abort
public void Abort()
webSocketClient.Abort()

Aborts the WebSocket connection immediately.

Connect
public void Connect()
webSocketClient.Connect()

Connects to the server synchronously.

ConnectAsync
public ValueTask ConnectAsync(CancellationToken cancellationToken)
result = webSocketClient.ConnectAsync(cancellationToken)

Asynchronously connects to the server.

cancellationToken
A token to monitor for cancellation requests.

Retorna: A task that represents the asynchronous connect operation.

Disconnect
public void Disconnect()
webSocketClient.Disconnect()

Disconnects from the server.

DisconnectAsync
public ValueTask DisconnectAsync(CancellationToken cancellationToken)
result = webSocketClient.DisconnectAsync(cancellationToken)

Asynchronously disconnects from the server.

cancellationToken
A token to monitor for cancellation requests.

Retorna: A task that represents the asynchronous disconnect operation.

DisposeManaged
protected override void DisposeManaged()
webSocketClient.DisposeManaged()

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

RemoveResend
public void RemoveResend(long subId)
webSocketClient.RemoveResend(subId)

Removes a resend command for the specified subscription identifier.

subId
The subscription identifier.
RemoveResend
public void RemoveResend()
webSocketClient.RemoveResend()

Removes all resend commands.

ResendAsync
public ValueTask ResendAsync(CancellationToken cancellationToken)
result = webSocketClient.ResendAsync(cancellationToken)

Asynchronously resends stored commands after reconnecting.

cancellationToken
A token to monitor for cancellation requests.

Retorna: A task that represents the asynchronous resend operation.

Send
public void Send(object obj, long subId, Func<long, CancellationToken, ValueTask> pre)
webSocketClient.Send(obj, subId, pre)

Sends an object to the server synchronously.

obj
The object to send. If not a byte array, it is converted to JSON.
subId
The subscription identifier.
pre
A pre-send callback function.
Send
public void Send(byte[] sendBuf, WebSocketMessageType type, long subId, Func<long, CancellationToken, ValueTask> pre)
webSocketClient.Send(sendBuf, type, subId, pre)

Sends a byte array message to the server synchronously.

sendBuf
The byte array to send.
type
The type of WebSocket message.
subId
The subscription identifier.
pre
A pre-send callback function.
SendAsync
public ValueTask SendAsync(object obj, long subId, Func<long, CancellationToken, ValueTask> pre)
result = webSocketClient.SendAsync(obj, subId, pre)

Asynchronously sends an object to the server.

obj
The object to send. If not a byte array, it is converted to JSON.
subId
The subscription identifier.
pre
A pre-send callback function.

Retorna: A task that represents the asynchronous send operation.

SendAsync
public ValueTask SendAsync(object obj, CancellationToken cancellationToken, long subId, Func<long, CancellationToken, ValueTask> pre)
result = webSocketClient.SendAsync(obj, cancellationToken, subId, pre)

Asynchronously sends an object to the server.

obj
The object to send. If not a byte array, it is converted to JSON.
cancellationToken
A token to monitor for cancellation requests.
subId
The subscription identifier.
pre
A pre-send callback function.

Retorna: A task that represents the asynchronous send operation.

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

Asynchronously sends a byte array message to the server.

sendBuf
The byte array to send.
type
The type of WebSocket message.
subId
The subscription identifier.
pre
A pre-send callback function.

Retorna: A task that represents the asynchronous send operation.

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

Asynchronously sends a byte array message to the server.

sendBuf
The byte array to send.
type
The type of WebSocket message.
cancellationToken
A token to monitor for cancellation requests.
subId
The subscription identifier.
pre
A pre-send callback function.

Retorna: A task that represents the asynchronous send operation.

SendOpCode
public ValueTask SendOpCode(byte code)
result = webSocketClient.SendOpCode(code)

Sends an operation code directly.

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

Retorna: A task that represents the asynchronous operation.

Eventos

Init
public event Action<ClientWebSocket> Init
webSocketClient.Init += handler

Occurs when the client web socket is initialized.

InitAsync
public event Func<ClientWebSocket, CancellationToken, ValueTask> InitAsync
webSocketClient.InitAsync += handler

Occurs when the client web socket is initialized, for a handler that has to be awaited.

PostConnect
public event Func<bool, CancellationToken, ValueTask> PostConnect
webSocketClient.PostConnect += handler

Occurs after a successful connection.

PreProcess2
public event Func<ReadOnlyMemory<byte>, Memory<byte>, int> PreProcess2
webSocketClient.PreProcess2 += handler

Occurs before processing received data. Input: original buffer (ReadOnlyMemory), output: destination buffer (Memory) returns processed length.

PreProcessAsync
public event Func<ReadOnlyMemory<byte>, Memory<byte>, CancellationToken, ValueTask<int>> PreProcessAsync
webSocketClient.PreProcessAsync += handler

Occurs before processing received data, for a transform that has to be awaited. Input: original buffer (ReadOnlyMemory), output: destination buffer (Memory) returns processed length.

Campos

DisconnectTimeout
public TimeSpan DisconnectTimeout
value = webSocketClient.DisconnectTimeout

Gets or sets the disconnect timeout period.