ResgateIO.Client 0.2.0

dotnet add package ResgateIO.Client --version 0.2.0
NuGet\Install-Package ResgateIO.Client -Version 0.2.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="ResgateIO.Client" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResgateIO.Client --version 0.2.0
#r "nuget: ResgateIO.Client, 0.2.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 ResgateIO.Client as a Cake Addin
#addin nuget:?package=ResgateIO.Client&version=0.2.0

// Install ResgateIO.Client as a Cake Tool
#tool nuget:?package=ResgateIO.Client&version=0.2.0

RES Client for .NET : Synchronize Your Clients

.NET client library implementing the RES-Client Protocol. Used to establish WebSocket connections to Resgate.

Visit Resgate.io for more information.
Visit the GitHub repository for the source code.

Basic usage

// Creating a client using a hostUrl string.
var client = new ResClient("ws://127.0.0.1:8080");

// Subscribing to a model of the default type ResModel.
var model = await client.SubscribeAsync("example.model") as ResModel;

// Accessing a model value by property.
Console.WriteLine("Model property foo: {0}", model["foo"]);

// Unsubscribing to a model (same as collection).
await client.UnsubscribeAsync("example.model");

// Getting a collection of the default type ResCollection.
var collection = await client.SubscribeAsync("example.collection") as ResCollection;

// Accessing a collection value by index.
Console.WriteLine("Collection value at index 0: {0}", collection[0]);

// Unsubscribing to a collection (same as model).
await client.UnsubscribeAsync("example.collection");

Defining a custom model class

/// <summary>
/// Mail represents a mail message resource model.
/// Used in the basic examples below.
/// </summary>
public class Mail : ResModelResource
{
	public string Subject { get; private set; }
	public string Sender { get; private set; }
	public string Body { get; private set; }

	public readonly ResClient Client;

	public Mail(ResClient client, string rid) : base(rid)
	{
		Client = client;
	}

	public override void Init(IReadOnlyDictionary<string, object> props)
	{
		Subject = props["subject"] as string;
		Sender = props["sender"] as string;
		Body = props["body"] as string;
	}

	public override void HandleEvent(ResourceEventArgs ev)
	{
		switch (ev)
		{
			case ModelChangeEventArgs changeEv:
				if (changeEv.NewValues.TryGetValue("subject", out object subject))
				{
					Subject = subject as string;
				}
				if (changeEv.NewValues.TryGetValue("sender", out object sender))
				{
					Sender = sender as string;
				}
				if (changeEv.NewValues.TryGetValue("body", out object body))
				{
					Body = body as string;
				}
				break;
		}
	}

	public async Task ReplyAsync(string message)
	{
		await Client.CallAsync(ResourceID, "reply", new
		{
			message = message
		});
	}
}

Using custom resource factories

// Creating a client using a hostUrl string.
var client = new ResClient("ws://127.0.0.1:8080");

// Registering mail model and collection factories for the patterns.
client.RegisterModelFactory("example.mail.*", (client, rid) => new Mail(client, rid));
client.RegisterCollectionFactory("example.mails", (client, rid) => new ResCollection<Mail>(client, rid));

// Getting a collection of registered types.
var mails = await client.SubscribeAsync("example.mails") as ResCollection<Mail>;

// Iterate over all mails
foreach (Mail mail in mails)
{
	Console.WriteLine("Mail: {0} {1}", mail.Subject, mail.Body);
}

Calling methods

// Creating a client using a hostUrl string
var client = new ResClient("ws://127.0.0.1:8080");

// Registering mail model factory
client.RegisterModelFactory("example.mail.*", (client, rid) => new Mail(client, rid));

// Calling a method assuming it returns a mail model in a resource response.
var mail = await client.CallAsync("example.mails", "getLastMail") as Mail;
var totalMails = await client.CallAsync<int>("example.mails", "getTotalMails");

// Call model method
await mail.ReplyAsync("This is my reply");

Listening to events

// Creating a client using a hostUrl string
var client = new ResClient("ws://127.0.0.1:8080");

// Listening for any resource event
client.ResourceEvent += (sender, e) =>
{
	Console.WriteLine("Event for resource {0}: {1}", e.ResourceID, e.EventName);
};

// Getting a model of the default type ResModel
var model = await client.SubscribeAsync("example.model") as ResModel;

// Listening for model change
EventHandler<ResourceEventArgs> handler = (sender, e) =>
{
	switch (e)
	{
		case ModelChangeEventArgs changeEvent:
			Console.WriteLine("Model change event");
			break;
		default:
			Console.WriteLine("Custom event: ", e.EventName);
			break;
	}
};
model.ResourceEvent += handler;
await Task.Delay(1000);
// Stop listening for model change
model.ResourceEvent -= handler;

Contributing

If you find any issues, feel free to report them as an Issue.

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. 
.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.

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
0.2.0 214 11/13/2023
0.1.3 108 10/12/2023
0.1.2 251 6/25/2023
0.1.1 258 3/29/2023
0.1.0 210 10/4/2022