locked
How to create mqtt client to publish/subscribe on web application(ASP.NET)? RRS feed

  • Question

  • User-1586767941 posted

    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.

    note:

    and i m using 

    this.Dispatcher.Invoke(() =>

    what is the alternative for same behaviour in asp.net.

    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 });
            }

     

    Tuesday, December 27, 2016 3:54 AM

All replies