nanoFramework.SignalR.Client 1.1.66

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package nanoFramework.SignalR.Client --version 1.1.66
NuGet\Install-Package nanoFramework.SignalR.Client -Version 1.1.66
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="nanoFramework.SignalR.Client" Version="1.1.66" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.SignalR.Client --version 1.1.66
#r "nuget: nanoFramework.SignalR.Client, 1.1.66"
#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 nanoFramework.SignalR.Client as a Cake Addin
#addin nuget:?package=nanoFramework.SignalR.Client&version=1.1.66

// Install nanoFramework.SignalR.Client as a Cake Tool
#tool nuget:?package=nanoFramework.SignalR.Client&version=1.1.66

Quality Gate Status Reliability Rating NuGet #yourfirstpr Discord

nanoFramework logo


document language: English | 简体中文

Welcome to the .NET nanoFramework SignalR Client Library repository

This API mirrors (as close as possible) the official .NET Microsoft.AspNetCore.SignalR.Client. Exceptions are mainly derived from the lack of async and generics support in .NET nanoFramework.

Build status

Component Build Status NuGet Package
nanoFramework.SignalR.Client Build Status NuGet

Usage

This is a SignalR Client library that enable you to connect your .net nanoFramework device to a SignalR Hub. SignalR is part of the ASP.NET Framework that makes it easy to create web applications that require high-frequency updates from the server like gaming. In the IoT domain SignalR can be used to create a webapp that for example shows a life graphs of connected smart meters, control a robot arm and many more.

Important: You must be connected to a network with a valid IP address. Please check the examples with the Network Helpers on how to set this up.

Connect to a hub

To establish a connection, create a HubConnection Client. You have to set the hub URL upon initialization of the HubConnection. You can also set custom headers by adding ClientWebsocketHeaders and set extra options by adding HubConnectionOptions upon initialization. The options are mainly used to change the settings of the underlying websocket and to set extra ssl options. You can start the connection by calling Start.

using System;
using System.Diagnostics;
using System.Threading;
using nanoFramework.SignalR.Client;

namespace NFSignalrTestClient
{
    public class Program
    {
        public static void Main()
        {
            //setup connection
            var options = new HubConnectionOptions() { Reconnect = true };
            HubConnection hubConnection = new HubConnection("http://YourSignalrTestServer/testhub", options: options);
            
            hubConnection.Closed += HubConnection_Closed;

            hubConnection.On("ReceiveMessage", new Type[] { typeof(string), typeof(string) }, (sender, args) =>
            {
                var name = (string)args[0];
                var message = (string)args[1];

                Console.WriteLine($"{name} : {message}");
            });
            
            //start connection
            hubConnection.Start();
                     

            AsyncResult dashboardClientConnected = hubConnection.InvokeCoreAsync("AwaitCientConnected", typeof(bool), new object[] { }, -1);

            int seconds = 0;

            while (!dashboardClientConnected.Completed)
            {
                Debug.WriteLine($"Waited {seconds} for client to open webapp");
                seconds++;
                Thread.Sleep(1000);
            }

            if ((bool)dashboardClientConnected.Value)
            {
                hubConnection.SendCore("ReportStatus", new object[] { "Client Connected" });

                int count = 0;
                while (hubConnection.State == HubConnectionState.Connected)
                {
                    hubConnection.InvokeCore("SendMessage", null, new object[] { count, "this is a control message" });
                    count++;
                    Thread.Sleep(1000);
                }
            }
            else
            {
                hubConnection.Stop("client failed to connect");
            }
        }

        private static void HubConnection_Closed(object sender, SignalrEventMessageArgs message)
        {
            Debug.WriteLine($"closed received with message: {message.Message}");
        }
    }
}
Handle lost connections

Reconnecting By default the HubConnection Client will not reconnect if a connection is lost or fails upon first connection. By setting the HubConnectionOptions Reconnect to true upon initialization of the HubConnection, the client will try to reconnect with a interval of 0, 2, 10, and 30 seconds, stopping after four failed attempts. When the client tries to reconnect the Reconnecting event is fired.

Note: the client will only try to reconnect if the connection is closed after receiving a server close message with the server request to reconnect.

Connection State

The connection state can be monitored by checking the HubConnection State. The different states are: Disconnected Connected Connecting and Reconnecting. After a connection is established every state change will fire a event: Closed Reconnecting or Reconnected. These events can be used to manual handle disconnects and reconnection.

Call Hub methods from Client

There are three ways to call a method on the hub. All three methods require you to pass the hub method name and any arguments defined in the hub method.

The simples form is calling SendCore This will call the method without expecting or waiting for any response from the server. It’s a ‘fire and forget’ method.

The second method is InvokeCore. InvokeCore requires you to pass the hub method name, the hub method return type, the arguments and an optional timeout in milliseconds. If no timeout is given the default ServerTimeout is used. This is a Synchronous method that will wait until the server replies. The returned object is of the type defined by the method return type argument. The casting of the object to the right type should be done manually. Note: if the hub method return type is void, the return type upon calling InvokeCore or InvokeCoreAsync should be null.

Note: set timeout to -1 to disable the server timeout. If no return message is received from the server this will wait indefinitely.

The third method is InvokeCoreAsync. This is the same as InvokeCore but than asynchronous. It will return an AsyncResult.

AsyncResult

The AsyncResult monitors the return message of the hub method. Upon completion Completed will be true. Upon completion the Value will hold the return object that needs to be cast to the right Type manually. Calling Value before completion will result in the awaiting of the server return. If an error occurs, Error will be true and the error message will be inside ErrorMessage.

AsyncResult dashboardClientConnected = hubConnection.InvokeCoreAsync("AwaitCientConnected", typeof(bool), new object[] { }, -1);

int seconds = 0;

while (!dashboardClientConnected.Completed)
{
    Debug.WriteLine($"Waited {seconds} for client to open webapp");
    seconds++;
    Thread.Sleep(1000);
}

if ((bool)dashboardClientConnected.Value)
{
    Debug.WriteLine("The client connected to the dashboard, start sending live data");
}

Call clients methods from hub

Define methods the hub calls using connection.On after building, but before starting the connection.

connection.On("ReceiveMessage", new Type[] { typeof(string), typeof(string) }, (sender, args) =>
{
    var name = args[0] as string;
    var message = args[1] as string;

    Debug.WriteLine($"{name} : {message}");
});

The preceding code in connection.On runs when server-side code calls it using the SendAsync method.

public async Task SendMessage(string user, string message)
{
    await Clients.All.SendAsync("ReceiveMessage", user, message);
}

Stopping the connection

The connection can be closed by calling Stop. This will stop the connection. If you want to stop the connection because for example a device sensor malfunctions, an error can be conveyed back to the server by stating the error using the optional errorMessage.

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
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
1.1.66 92 4/4/2024
1.1.64 108 2/1/2024
1.1.62 78 1/26/2024
1.1.59 195 11/10/2023
1.1.57 169 5/29/2023
1.1.54 140 5/19/2023
1.1.49 254 2/20/2023
1.1.46 241 1/26/2023
1.1.44 272 12/28/2022
1.1.40 419 11/24/2022
1.1.38 311 11/15/2022
1.1.36 376 10/28/2022
1.1.34 352 10/27/2022
1.1.32 347 10/25/2022
1.1.30 352 10/25/2022
1.1.26 390 10/24/2022
1.1.24 380 10/21/2022
1.1.22 368 10/12/2022
1.1.20 382 10/11/2022
1.1.18 381 9/29/2022
1.1.14 425 9/23/2022
1.1.12 405 9/23/2022
1.1.10 469 9/18/2022
1.1.8 428 9/16/2022
1.1.6 396 9/8/2022
1.1.4 406 8/6/2022
1.1.2 407 8/5/2022
1.0.0 447 3/29/2022
1.0.0-preview.93 124 3/29/2022
1.0.0-preview.91 114 3/29/2022
1.0.0-preview.89 122 3/18/2022
1.0.0-preview.87 113 3/17/2022
1.0.0-preview.85 112 3/15/2022
1.0.0-preview.83 108 3/14/2022
1.0.0-preview.81 111 3/14/2022
1.0.0-preview.79 101 3/14/2022
1.0.0-preview.77 105 3/14/2022
1.0.0-preview.75 111 3/14/2022
1.0.0-preview.73 114 3/10/2022
1.0.0-preview.71 103 3/9/2022
1.0.0-preview.69 102 3/6/2022
1.0.0-preview.67 114 3/5/2022
1.0.0-preview.65 111 3/4/2022
1.0.0-preview.63 106 3/3/2022
1.0.0-preview.61 104 3/1/2022
1.0.0-preview.59 116 2/26/2022
1.0.0-preview.57 115 2/24/2022
1.0.0-preview.55 115 2/18/2022
1.0.0-preview.53 108 2/18/2022
1.0.0-preview.50 115 2/17/2022
1.0.0-preview.48 116 2/13/2022
1.0.0-preview.46 121 2/12/2022
1.0.0-preview.44 114 2/11/2022
1.0.0-preview.42 111 2/9/2022
1.0.0-preview.40 110 2/8/2022
1.0.0-preview.38 105 2/7/2022
1.0.0-preview.36 120 2/6/2022
1.0.0-preview.33 126 2/5/2022
1.0.0-preview.31 130 2/1/2022
1.0.0-preview.28 122 1/31/2022