MessagePump.OnMessage Event

To receive live messages

Namespace: Gamanet.C4.SDK
Assembly: Gamanet.C4.SimpleClient (in Gamanet.C4.SimpleClient.dll) Version: 2026.0.0.733
Syntax
C#
public event EventHandler<MessageEventArgsV1> OnMessage

Value

EventHandler<MessageEventArgsV1>

Implements

ISimpleClientMessagePumpV1.OnMessage
Example
The following example illustrates how to subscribe to OnMessage.
C#
using System;
using Gamanet.C4.SDK;
using Gamanet.C4.SDK.Enums.Devices;
using Gamanet.C4.SimpleInterfaces;

public class OnMessage
{
    public static void Main()
    {
        // create instance of simple client
        var client1 = new SimpleClient(ConnectorConfiguration.HttpClient, null);
        var client2 = new SimpleClient(ConnectorConfiguration.HttpClient, null);
        ConnectionInfo token;

        // connect to local server
        var connectionResult = client1.Connect(new Uri("https://localhost/"), "Support", "support", out token);
        var connectionResult1 = client2.Connect(new Uri("https://localhost/"), "Support", "support", out token);
        if (connectionResult != ConnectionResult.Successful && connectionResult1 != ConnectionResult.Successful)
        {
            Console.WriteLine("Connection unSuccessful, result: {0}", connectionResult);
            return;
        }

        Console.WriteLine("Connection result: {0}", connectionResult);
        Console.WriteLine();

        // new device tree
        var busConrtollerDevice = new SimpleDevice(Guid.NewGuid(), "Bus Controller", EntityRoot.Device,
            Guid.Parse("D81A3534-CA3F-4998-A8AC-473143DA521E"));
        var panel = new SimpleDevice(Guid.NewGuid(), "EDGE EVE E440 Sector 1", busConrtollerDevice.Id,
            Guid.Parse("3C684E0C-AF67-4129-89AA-833C0F466032"));
        var door = new SimpleDevice(Guid.NewGuid(), "Door", panel.Id,
            Guid.Parse("0940228b-69c9-4d0a-a804-1e96c82568e3"));

        // create devices
        client1.Devices.Create(busConrtollerDevice);
        client1.Devices.Create(panel);
        client1.Devices.Create(door);

        // create new person under person root
        var person = new SimplePerson(
            Guid.NewGuid(),
            "Johnson",
            EntityRoot.Person,
            PersonCategory.Person);

        person.Data["Surname"] = "Luck";

        // create person in db
        client1.Persons.Create(person);

        // start message pump
        client1.MessagePump.Start();
        client2.MessagePump.Start();

        // start handling message pump incoming messages
        client2.MessagePump.OnMessage += OnMessageFunc;

        // create log
        var log = new SimpleLog(Guid.NewGuid(), "", Guid.Empty, LogCategory.DoorOpened)
        {
            Data = new Dictionary<string, object>()
            {
                ["CategoryId"] = door.CategoryId,
                ["ParentId"] = door.ParentId,
                ["DeviceIdName"] = door.Name,
                ["DeviceId"] = door.Id,
                ["PersonIdName"] = person.Name
            }
        };

        // create message for log
        var message = new Message(
            Guid.Empty,
            Guid.Empty,
            log); // log for transfer

        // send message
        client1.MessagePump.Send(message);

        Console.WriteLine("Log sent");

        // stop handling messages
        client1.MessagePump.OnMessage -= OnMessageFunc;

        // stop message pump
        client1.MessagePump.Stop();
        client2.MessagePump.Stop();

        // disconnect from server
        client1.Disconnect();
        client2.Disconnect();

        // The example displays the following output:
        // Connection result: Successful

        // Log sent
        // Message Pump state: Started
        // LOG  'Door' opened by 'Johnson'. | LogCategory 8cbda41e - 753f - 46f3 - aa4c - f3468109b9e7
        // Message Pump state: Stopped
        Console.ReadLine();
    }

    private static void OnMessageFunc(object sender, MessageEventArgs e)
    {
        if (e.Message.CategoryId == MessageCategory.Status)
        {
            var status = (SimpleStatus)e.Message.SimpleEntity;
            Console.WriteLine($"STATUS  {status.Name} | StatusCategory {status.CategoryId}");
        }
        else if (e.Message.CategoryId == MessageCategory.Command)
        {
            var sc = (SimpleCommand)e.Message.SimpleEntity;
            Console.WriteLine($"COMMAND  {sc.Name} | CommandCategory {sc.CategoryId}");
        }
        else if (e.Message.CategoryId == MessageCategory.Log)
        {
            var sc = (SimpleLog)e.Message.SimpleEntity;
            Console.WriteLine($"LOG  {sc.Name} | LogCategory {sc.CategoryId}");
        }
    }
}
See Also

Reference

MessagePump Class
Gamanet.C4.SDK Namespace
MessageV1
MessageCategory
SimpleLogV1
SimpleStatusV1
SimpleCommandV1