locked
ASP.NET Event Handler problem RRS feed

  • Question

  • User1324033403 posted

    Hello all,

    I am using a M2Mqtt client in Web Forms for publishing and receiving messages from a MQTT Broker. My code behind is as follows:

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using uPLibrary.Networking.M2Mqtt;
    using uPLibrary.Networking.M2Mqtt.Messages;
    
    namespace mqtt_app
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            public MqttClient MqttClient = new MqttClient("xxx.xxx.xxx.x");
    
            public void Page_Load(object sender, EventArgs e)
            {
    
            }
                
            protected void Button1_Click1(object sender, EventArgs e)
            {
                MqttClient.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
                string clientID = Guid.NewGuid().ToString();
                MqttClient.Connect(clientID);
                // Subscribe
                MqttClient.Subscribe(new string[] { "tinkerforge/response/accelerometer_v2_bricklet/Jej/get_acceleration" }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
                MqttClient.Publish("tinkerforge/request/accelerometer_v2_bricklet/Jej/get_acceleration", Encoding.UTF8.GetBytes(""), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);
                
            }
    
            public void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
            {
                var message = System.Text.Encoding.Default.GetString(e.Message);
                System.Diagnostics.Debug.WriteLine(message);
                dynamic akceleracija = JsonConvert.DeserializeObject(message);
                int x = akceleracija.x;
                System.Diagnostics.Debug.WriteLine(Convert.ToString(x));
                int y = akceleracija.y;
                System.Diagnostics.Debug.WriteLine(Convert.ToString(y));
                int z = akceleracija.z;
                System.Diagnostics.Debug.WriteLine(Convert.ToString(z));
                Label1.Text = Convert.ToString(x);
                Label2.Text = Convert.ToString(y);
                Label3.Text = Convert.ToString(z); } } }

    I am able to successfully subscribe and publish a message to broker, which is then received in a Client_MqttMsgPublishReceived as JSON string {"z": 10282, "y": 269, "x": 90}. Using the JsonConvert.DeserializeObject I can extract the contents of the message, which I want to display inside 3 labels. My problem is that Debug shows all of the values but they are not displayed on the web form when I click a button. All labels are asp label and have runat="server" included. Also when I run the program no error are shown. 

    Does anybody have any suggestions on what I should do?

    Thursday, October 29, 2020 9:51 AM

All replies