Hi i published a message from paho client to MQTT broker and want to subscribe that message using that topic ; my problem is when debugger comes to the line client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; it is not executing the method and moving to the next line.
I use hive mqtt broker and c# Form Application with M2MQTT libraries added.
I want to get the published message in the text box. will you people help me please.
Here is my code;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
// including the M2Mqtt Library
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
private void button1_Click(object sender, EventArgs e)
{
MqttClient client = new MqttClient("IP.ADDRESS");
client.ProtocolVersion = MqttProtocolVersion.Version_3_1;
byte code = client.Connect(Guid.NewGuid().ToString());
ushort msgIds = client.Subscribe(new string[] { @"Factory1\Sensor1" },
new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
textBox1.Text = "";
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
this.Invoke((MethodInvoker)delegate ()
{
textBox1.Text += "Received = " + Encoding.UTF8.GetString(e.Message) + "
on topic " + e.Topic + "\r\n";
});
}