tar.WebSocket 1.1.0

dotnet add package tar.WebSocket --version 1.1.0                
NuGet\Install-Package tar.WebSocket -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="tar.WebSocket" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add tar.WebSocket --version 1.1.0                
#r "nuget: tar.WebSocket, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

alternate text is missing from this package README image alternate text is missing from this package README image

  • 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 or Aborted
  • 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 callback
  • ClientActionDescription: action as text
  • Closed: last closed timestamp
  • Duration: time the web socket is/was open
  • ErrorMessage: description when an error occured
  • Opened: last opened timestamp
  • ReceivedMessage: received message as JSON string
  • SentMessage: sent message as JSON string
  • SentOptions: optional options object you have provided in SendAsync()
  • SentPayload: optional payload object you have provided in SendAsync()
  • State: the current state of the internal ClientWebSocket
  • StateDescription: state as text
  • Success: if the action was successful
  • Timestamp: when the action occured
  • TriggeredByClient: if the action was triggered by the client (you), otherwise by the server
  • Url: the URL the web socket is connected to

The provided information depends on the actual client action.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last updated
1.1.0 162 12/28/2024
1.0.0 85 12/27/2024