Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
I am using HiveMQ broker to publish and subscribe. so I created wpf application to get the message it working fine.
and same thing I tried with ASP.net But it not working. while debugging I am able to get the data but not displaying it on form.
Is there any other way to do this on web application or am I missing some thing? Plz give some idea.

What I have tried:

C#
private MqttClient mqttClient;
public MainWindow()
        {
            InitializeComponent();
            mqttClient = new            MqttClient(IPAddress.Parse(txtConnectionString.Text).ToString());
            mqttClient.MqttMsgPublishReceived += client_MqttMsgPublishReceived;//event
            mqttClient.MqttMsgSubscribed += client_MqttMsgSubscribed;//event
        }

private void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                lstSubscribe.Items.Add("Recevied..." +   System.Text.Encoding.UTF8.GetString(e.Message));
            });
        }

        private void client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                lstSubscribe.Items.Add("Topic " + txtTopic.Text);
                 lstSubscribe.Items.Add("Subscribed for id " + e.MessageId);
                 lstSubscribe.Items.Add(" ");
            });
        }

private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                mqttClient.Connect(Guid.NewGuid().ToString());

                lstPublish.Items.Add("*** Connected");
            }
            catch (Exception ex)
            {
                lblError.Content = ex.Message.ToString();
            }
        }

        private void btnSubscribe_Click(object sender, RoutedEventArgs e)
        {
            mqttClient.Subscribe(new string[] { txtTopic.Text }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
        }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900