WebSocketClient
Represents a client for WebSocket connections.
継承元: Disposable
実装: IConnection
コンストラクター
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.
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.
プロパティ
public int BufferSize { get; set; }
value = webSocketClient.BufferSize
webSocketClient.BufferSize = value
Gets or sets the buffer size for compressed data.
public int BufferSizeUncompress { get; set; }
value = webSocketClient.BufferSizeUncompress
webSocketClient.BufferSizeUncompress = value
Gets or sets the buffer size for uncompressed data.
public bool DisableAutoResend { get; set; }
value = webSocketClient.DisableAutoResend
webSocketClient.DisableAutoResend = value
Gets or sets a value that indicates whether auto resend is disabled.
public Encoding Encoding { get; set; }
value = webSocketClient.Encoding
webSocketClient.Encoding = value
Gets or sets the encoding used to convert between bytes and string data.
public bool Indent { get; set; }
value = webSocketClient.Indent
webSocketClient.Indent = value
Gets or sets a value that indicates whether output JSON should be indented.
public bool IsConnected { get; }
value = webSocketClient.IsConnected
Gets a value indicating whether the client is connected.
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.
public TimeSpan ReconnectInterval { get; set; }
value = webSocketClient.ReconnectInterval
webSocketClient.ReconnectInterval = value
Gets or sets the interval between reconnection attempts.
public TimeSpan ResendInterval { get; set; }
value = webSocketClient.ResendInterval
webSocketClient.ResendInterval = value
Gets or sets the interval between resend attempts for messages.
public TimeSpan ResendTimeout { get; set; }
value = webSocketClient.ResendTimeout
webSocketClient.ResendTimeout = value
Gets or sets the timeout before resend of commands after reconnection.
public JsonSerializerSettings SendSettings { get; set; }
value = webSocketClient.SendSettings
webSocketClient.SendSettings = value
Gets or sets the JSON serializer settings used when sending objects.
public ConnectionStates State { get; private set; }
value = webSocketClient.State
webSocketClient.State = value
Gets the current connection state.
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.
メソッド
public ValueTask ConnectAsync(CancellationToken cancellationToken)
result = webSocketClient.ConnectAsync(cancellationToken)
Asynchronously connects to the server.
- cancellationToken
- A token to monitor for cancellation requests.
戻り値: A task that represents the asynchronous connect operation.
public ValueTask DisconnectAsync(CancellationToken cancellationToken)
result = webSocketClient.DisconnectAsync(cancellationToken)
Asynchronously disconnects from the server.
- cancellationToken
- A token to monitor for cancellation requests.
戻り値: A task that represents the asynchronous disconnect operation.
protected override void DisposeManaged()
webSocketClient.DisposeManaged()
Disposes the managed resources. Override this method to add custom clean up of managed resources.
public void RemoveResend(long subId)
webSocketClient.RemoveResend(subId)
Removes a resend command for the specified subscription identifier.
- subId
- The subscription identifier.
public ValueTask ResendAsync(CancellationToken cancellationToken)
result = webSocketClient.ResendAsync(cancellationToken)
Asynchronously resends stored commands after reconnecting.
- cancellationToken
- A token to monitor for cancellation requests.
戻り値: A task that represents the asynchronous resend operation.
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.
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.
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.
戻り値: A task that represents the asynchronous send operation.
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.
戻り値: A task that represents the asynchronous send operation.
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.
戻り値: A task that represents the asynchronous send operation.
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.
戻り値: A task that represents the asynchronous send operation.
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).
戻り値: A task that represents the asynchronous operation.
イベント
public event Action<ClientWebSocket> Init
webSocketClient.Init += handler
Occurs when the client web socket is initialized.
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.
public event Func<bool, CancellationToken, ValueTask> PostConnect
webSocketClient.PostConnect += handler
Occurs after a successful connection.
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.
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.
フィールド
public TimeSpan DisconnectTimeout
value = webSocketClient.DisconnectTimeout
Gets or sets the disconnect timeout period.