tar.WebSocket
1.1.0
dotnet add package tar.WebSocket --version 1.1.0
NuGet\Install-Package tar.WebSocket -Version 1.1.0
<PackageReference Include="tar.WebSocket" Version="1.1.0" />
paket add tar.WebSocket --version 1.1.0
#r "nuget: tar.WebSocket, 1.1.0"
// Install tar.WebSocket as a Cake Addin #addin nuget:?package=tar.WebSocket&version=1.1.0 // Install tar.WebSocket as a Cake Tool #tool nuget:?package=tar.WebSocket&version=1.1.0
tar.WebSocket
- C# .NET Standard v2.0
Function
This library can be used as web socket client.
It basically wraps the functionality of System.Net.WebSockets.ClientWebSocket
but additionally provides:
- the correct close handling when the client or the server triggers the closure
- the possibility to re-connect after the web socket state has once been set to
Closed
orAborted
- an
OnAction
event which can be subscribed for callbacks whenever any of the following action occurs:- Closing
- Connecting
- MessageReceived
- MessageSent
- StateChanged
Usage
var webSocketClient = new WebSocketClient(
url: "wss://ws.example.com/v1/"
);
You can use the web socket client as usual:
await webSocketClient.ConnectAsync();
await webSocketClient.CloseAsync();
await webSocketClient.SendAsync(jsonString);
webSocketClient.Abort();
You are able to forward an optional optionsObject and payloadObject to SendAsync()
which are returned via callback in the OnAction
event.
Options
Additionaly, you can set options as usual and they will be adopted.
var options = new WebSocketClientOptions() {
KeepAliveInterval = TimeSpan.FromSeconds(5);
};
var webSocketClient = new WebSocketClient(
options: options
url: "wss://ws.example.com/v1/"
);
If no options or no KeepAliveInterval
are set, a KeepAliveInterval
of 1 second will be used.
After instanciation you cannot adjust the options but you can use the usual options methods on the client directly:
webSocketClient.AddSubProtocol(subProtocol);
webSocketClient.SetBuffer(receiveBufferSize, sendBufferSize);
webSocketClient.SetBuffer(receiveBufferSize, sendBufferSize, buffer);
webSocketClient.SetRequestHeader(headerName, headerValue);
Be aware, that you may still need to re-call those after an existing connection has been aborted/closed.
Callback Event OnAction
To receive updates, you need to add an event handler for the event OnAction
and subscribe it.
For example:
// register/subscribe to event
webSocketClient.OnAction += OnWebSocketClientAction;
// your callback method which is triggered via the event
private void OnWebSocketClientAction(WebSocketClientInfo info) {
switch (info.ClientAction) {
case WebSocketClientAction.Closing: OnClosing(info); break;
case WebSocketClientAction.Connecting: OnConnecting(info); break;
case WebSocketClientAction.MessageReceived: OnMessageReceived(info); break;
case WebSocketClientAction.MessageSent: OnMessageSent(info); break;
case WebSocketClientAction.StateChanged: OnStateChanged(info); break;
}
}
// your explicit method where you handle on closing events
private void OnClosing(WebSocketClientInfo info) {
MessageBox.Show(
info.Success
? "Connection closed"
: $"Connection not closed: {info.ErrorMessage}"
);
}
// etc.
The returned WebSocketClientInfo
class contains all necessary information:
ClientAction
: action which triggered the callbackClientActionDescription
: action as textClosed
: last closed timestampDuration
: time the web socket is/was openErrorMessage
: description when an error occuredOpened
: last opened timestampReceivedMessage
: received message as JSON stringSentMessage
: sent message as JSON stringSentOptions
: optional options object you have provided inSendAsync()
SentPayload
: optional payload object you have provided inSendAsync()
State
: the current state of the internal ClientWebSocketStateDescription
: state as textSuccess
: if the action was successfulTimestamp
: when the action occuredTriggeredByClient
: if the action was triggered by the client (you), otherwise by the serverUrl
: the URL the web socket is connected to
The provided information depends on the actual client action.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on tar.WebSocket:
Package | Downloads |
---|---|
tar.Bitvavo.Api
Full Bitvavo API with REST and WebSocket functionality. |
GitHub repositories
This package is not used by any popular GitHub repositories.