EdjCase.ICP.WebSockets
6.1.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package EdjCase.ICP.WebSockets --version 6.1.0
NuGet\Install-Package EdjCase.ICP.WebSockets -Version 6.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="EdjCase.ICP.WebSockets" Version="6.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EdjCase.ICP.WebSockets --version 6.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EdjCase.ICP.WebSockets, 6.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 EdjCase.ICP.WebSockets as a Cake Addin #addin nuget:?package=EdjCase.ICP.WebSockets&version=6.1.0 // Install EdjCase.ICP.WebSockets as a Cake Tool #tool nuget:?package=EdjCase.ICP.WebSockets&version=6.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ICP.NET WebSockets
Usage (Minimal)
Principal canisterId = ...; // Id of the canister to communicate with over websockets
Uri gatewayUri = ...; // Uri to the websocket gateway server
IWebSocketAgent<AppMessage> webSocket = await new WebSocketBuilder(canisterId, gatewayUri)
.OnMessage((msg) => {}) // Action to receive messages from the server
.BuildAndConnectAsync<AppMessage>();
Usage (Advanced)
IWebSocketAgent<AppMessage> webSocket = await new WebSocketBuilder(canisterId, gatewayUri)
.OnMessage((msg) => {}) // Action to receive messages from the server
.OnOpen(() => {}) // Action to indicate when the connection opens
.OnError((ex) => {}) // Action to output an error
.OnClose(() => {}) // Action to indicate when the connection closes
.WithIdentity(identity) // Used to specify an IIdentity, otherwise will generate an ephemeral identity
.WithRootKey(rootKey) // Used to specify the network root public key, required if using a dev or other ic network
.WithCustomCandidConverter(customConverter) // Used if `AppMessage` requires a custom CandidConverter for serialization
.WithCustomClient(client) // Used to override the default WebSocket client
.WithCustomBlsCryptography(bls) // Used to override the default bls library.
.BuildAndConnectAsync<AppMessage>(cancellationToken);
Unity
Example using MonoBehavior
using EdjCase.ICP.Agent.Agents;
using EdjCase.ICP.Candid.Models;
using EdjCase.ICP.WebSockets;
using System.Threading;
using System;
using UnityEngine;
using EdjCase.ICP.Agent;
using EdjCase.ICP.Candid.Mapping;
public class WebSocketManager : MonoBehaviour
{
public class AppMessage
{
[CandidName("text")]
public string Text { get; set; }
[CandidName("timestamp")]
public ulong Timestamp { get; set; }
}
private Principal devCanisterId = Principal.FromText("bkyz2-fmaaa-aaaaa-qaaaq-cai");
private Uri devGatewayUri = new Uri("ws://localhost:8080");
private Uri devBoundryNodeUri = new Uri("http://localhost:4943");
private Principal prodCanisterId = Principal.FromText("bkyz2-fmaaa-aaaaa-qaaaq-cai");
private Uri prodGatewayUri = new Uri("wss://icwebsocketgateway.app.runonflux.io");
private IWebSocketAgent<AppMessage> agent;
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
async void Start()
{
bool development = true;
Principal canisterId;
Uri gatewayUri;
if (development)
{
canisterId = devCanisterId;
gatewayUri = devGatewayUri;
}
else
{
canisterId = prodCanisterId;
gatewayUri = prodGatewayUri;
}
var builder = new WebSocketBuilder<AppMessage>(canisterId, gatewayUri)
.OnMessage(this.OnMessage)
.OnOpen(this.OnOpen)
.OnError(this.OnError)
.OnClose(this.OnClose);
if (development)
{
// Set the root key as the dev network key
SubjectPublicKeyInfo devRootKey = await new HttpAgent(
httpBoundryNodeUrl: devBoundryNodeUri
).GetRootKeyAsync();
builder = builder.WithRootKey(devRootKey);
}
this.agent = await builder.BuildAndConnectAsync(cancellationToken: cancellationTokenSource.Token);
await this.agent.ReceiveAllAsync(cancellationTokenSource.Token);
}
void OnOpen()
{
Debug.Log("Open");
}
async void OnMessage(AppMessage message)
{
Debug.Log("Received Message: "+ message.Text);
ICTimestamp.Now().NanoSeconds.TryToUInt64(out ulong now);
var replyMessage = new AppMessage
{
Text = "pong",
Timestamp = now
};
await this.agent.SendAsync(replyMessage, cancellationTokenSource.Token);
Debug.Log("Sent Message: " + replyMessage.Text);
}
void OnError(Exception ex)
{
Debug.Log("Error: " + ex);
}
void OnClose()
{
Debug.Log("Close");
}
async void OnDestroy()
{
cancellationTokenSource.Cancel(); // Cancel any ongoing operations
if (this.agent != null)
{
await this.agent.DisposeAsync();
}
}
}
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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.1
- EdjCase.ICP.Agent (>= 6.1.0)
- Moq (>= 4.18.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
7.0.0-pre.1 | 40 | 10/27/2024 |
6.2.1 | 81 | 10/23/2024 |
6.2.0 | 90 | 10/21/2024 |
6.1.2 | 128 | 4/30/2024 |
6.1.1 | 116 | 4/17/2024 |
6.1.0 | 99 | 4/15/2024 |
6.0.0 | 124 | 3/21/2024 |
5.1.0 | 135 | 1/25/2024 |
5.0.0 | 136 | 1/12/2024 |
5.0.0-pre.2 | 122 | 12/13/2023 |