Team XSockets.NET

Communication Extensions

We have been using communication extensions all over the documentation. All samples from server-side code where we use Invoke, InvokeTo, InvokeToAll are RPC/RMI extensions. All samples with Publish, PublishTo and PublishToAll are Publish/Subscribe extensions.

A Custom Extension

Below we see the simple code of 2 controllers and a simple extension that let us target clients on a specific controller based on state.

The 2 controllers (sensor and monitor) is very basic. The Sensor controller has a single method that takes a sensor value and sends it to the custom extension named SendToMonitoringClients. The Monitor controller only has a property (Threshold), this property is used in the custom extension to target clients that has the threshold set to

The custom extension is decorated with where T : class, IXSocketController so that we can call this from any XSockets controller (not only the Sensor controller).

Code

using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
using System.Threading.Tasks;
using XSockets.Core.Common.Socket;

/// <summary>
/// Simple sensor controller the dispatches data to monitoring clients with our custom extension
/// </summary>
public class Sensor : XSocketController
{
    public async Task Data(int value)
    {
        await this.SendToMonitoringClients(value);
    }
}

/// <summary>
/// A controller for monitoring clients that has a individual threshold
/// </summary>
public class Monitor : XSocketController
{
    /// <summary>
    /// We use this to send data to client when the data is => the threshold
    /// </summary>
    public int Threshold { get; set; }
}

public static class MyCommExtentions
{
    /// <summary>
    /// Extension for targeting clients on a specific controller (Monitor) with a specific state (Threshold less or equal to value)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="controller"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static async Task SendToMonitoringClients<T>(this T controller, int value) where T : class, IXSocketController
    {
        await controller.InvokeTo<Monitor>(p => p.Threshold <= value, value, "sensordata");
    }
}

Test

To test our new extension we will open up 3 instances of Putty. One will act as a sensor, and the other two as a monitors with individual thresholds.

Monitor Client Nr1

The first client will connect to the monitor controller and set the threshold to 5

Monitor Client Nr2

The second client will connect to the monitor controller and set the threshold to 15

Sensor Client

This client will connect to the sensor controller and send in 3 values.

  • 3 - This message will not reach the monitoring client since it is below both thresholds
  • 10 - This message will reach one of the monitoring clients since 10 is > 5
  • 23 - This messgae will reach both monitoring clients since 23 > 5 && 23 > 15

Custom Extension Sample

results matching ""

    No results matching ""