Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hi all!

So I'm working on a "system controller"/console application that shall monitor and control a few control units via MQTT.
Connecting to a cloud based (right now ...) broker/server (tried both HiveMQ and emqx).

In the application I have, as of right now, three client instances contained in separate own developed "client handlers" that add some extra functionality on top of the MQTTnet client.

I'm adding two event functions to all three of these clients for handling incoming MQTT messages/topics.

The code looks like this:

                if (_firstConnect)
                {
                    // What happens if the same message function is added more than ones ???
                    _mqttClient.ApplicationMessageReceivedAsync -= LogReceivedMessages;
                    _mqttClient.ApplicationMessageReceivedAsync += LogReceivedMessages;
                    if (receivedMessageEvent != null)
                    {
                        _mqttClient.ApplicationMessageReceivedAsync -= receivedMessageEvent;
                        _mqttClient.ApplicationMessageReceivedAsync += receivedMessageEvent;
                    }
                    // => The method/function is triggered more than ones !!!
                    // => First always removing the function and then add it you don't get this behavior!
                    _firstConnect = false;
                }

It's "in development" so I have both a check if there are "first connect" if doing the connect for the first time and both removing and adding the two functions (first LogReceivedMessages and second receivedMessageEvent) as of now (what is best I also do not know :-) ..).

The log function looks like this:

    private async Task LogReceivedMessages(MqttApplicationMessageReceivedEventArgs e)
    {
        if (LogMessages)
        {
            Log($"---> Received message ...");
            // Log($"From ClientId: {e.ClientId}"); // TODO: Can you get what client that sent the message ??? 
            Log($"Topic received: {e.ApplicationMessage.Topic}");
            Log($"Payload/message received: {e.ApplicationMessage.ConvertPayloadToString()}");

            // TODO: How get whole message content by default ???
            // Log($"Event args: {e.ToString}");
            // Log($"Event args: {e.}");
        }
    }

and is for being able to log incoming messages if that is activated.

The second looks like this and is added as reference via the "receivedMessageEvent" function variable:

    private async Task ReceivedMQTTMessageEventStoreLocalCom(MqttApplicationMessageReceivedEventArgs args)
    {
        // Handle messages from local broker
        throw new NotImplementedException("ReceivedMQTTMessageEventStoreLocalCom");
    }

So this one throws an exception!

Now to the questions:

  1. The exception do not show up in the the console application windows but can be seen in the Visual Studio 2022 window for the debug output. Why?

  2. If I change the order of how the events are added (so instead first adding receivedMessageEvent and second LogReceivedMessages) the second, LogReceivedMessages, are not triggered at all. So the first function are triggered which can be seen from the debug output. BUT then the second function, LogReceivedMessages, is skipped/not triggered?
    Are this as it should? Or are the try and catch in your implementation wrongly situated!? Unsure how this works in the MQTTnet implementation ...

Thx in advance for answering!

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.