nanoFramework.SignalR.Client 1.0.0-preview.85

Prefix Reserved
This is a prerelease version of nanoFramework.SignalR.Client.
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.SignalR.Client --version 1.0.0-preview.85                
NuGet\Install-Package nanoFramework.SignalR.Client -Version 1.0.0-preview.85                
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.0.0-preview.85" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.SignalR.Client --version 1.0.0-preview.85                
#r "nuget: nanoFramework.SignalR.Client, 1.0.0-preview.85"                
#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.0.0-preview.85&prerelease

// Install nanoFramework.SignalR.Client as a Cake Tool
#tool nuget:?package=nanoFramework.SignalR.Client&version=1.0.0-preview.85&prerelease                

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


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
nanoFramework.SignalR.Client (preview) 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<string, string>("ReceiveMessage", (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.78 80 11/4/2024
1.1.76 96 10/3/2024
1.1.75 85 9/30/2024
1.1.74 96 9/9/2024
1.1.71 82 8/1/2024
1.1.68 113 6/3/2024
1.1.66 132 4/4/2024
1.1.64 134 2/1/2024
1.1.62 108 1/26/2024
1.1.59 220 11/10/2023
1.1.57 234 5/29/2023
1.1.54 159 5/19/2023
1.1.49 278 2/20/2023
1.1.46 266 1/26/2023
1.1.44 294 12/28/2022
1.1.40 447 11/24/2022
1.1.38 337 11/15/2022
1.1.36 396 10/28/2022
1.1.34 376 10/27/2022
1.1.32 369 10/25/2022
1.1.30 377 10/25/2022
1.1.26 417 10/24/2022
1.1.24 405 10/21/2022
1.1.22 389 10/12/2022
1.1.20 404 10/11/2022
1.1.18 422 9/29/2022
1.1.14 451 9/23/2022
1.1.12 430 9/23/2022
1.1.10 500 9/18/2022
1.1.8 453 9/16/2022
1.1.6 433 9/8/2022
1.1.4 431 8/6/2022
1.1.2 444 8/5/2022
1.0.0 487 3/29/2022
1.0.0-preview.93 136 3/29/2022
1.0.0-preview.91 124 3/29/2022
1.0.0-preview.89 131 3/18/2022
1.0.0-preview.87 123 3/17/2022
1.0.0-preview.85 121 3/15/2022
1.0.0-preview.83 119 3/14/2022
1.0.0-preview.81 122 3/14/2022
1.0.0-preview.79 111 3/14/2022
1.0.0-preview.77 116 3/14/2022
1.0.0-preview.75 124 3/14/2022
1.0.0-preview.73 127 3/10/2022
1.0.0-preview.71 117 3/9/2022
1.0.0-preview.69 117 3/6/2022
1.0.0-preview.67 128 3/5/2022
1.0.0-preview.65 126 3/4/2022
1.0.0-preview.63 119 3/3/2022
1.0.0-preview.61 119 3/1/2022
1.0.0-preview.59 127 2/26/2022
1.0.0-preview.57 126 2/24/2022
1.0.0-preview.55 125 2/18/2022
1.0.0-preview.53 118 2/18/2022
1.0.0-preview.50 125 2/17/2022
1.0.0-preview.48 126 2/13/2022
1.0.0-preview.46 130 2/12/2022
1.0.0-preview.44 124 2/11/2022
1.0.0-preview.42 122 2/9/2022
1.0.0-preview.40 120 2/8/2022
1.0.0-preview.38 116 2/7/2022
1.0.0-preview.36 131 2/6/2022
1.0.0-preview.33 138 2/5/2022
1.0.0-preview.31 145 2/1/2022
1.0.0-preview.28 132 1/31/2022