Sending a message to Xamarin Forms via Broker

Viewed 21

I am using MQTT.fx and

"test.mosquitto.org"

locally as a broker. Now i want to send a message from the broker to Visual Studio (where I am connected with the client/broker). How does that work? What I have since now is:

Edit: Obviously

//await client.PublishAsync(new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes($"{message}")), MqttQualityOfService.ExactlyOnce); //QOS 2

is not working at the moment...

private async void OnButton2Clicked(object sender, EventArgs e)
        {
            try 
            {   
                testLabel2.Text = "test";
                await client.SubscribeAsync(topic, MqttQualityOfService.ExactlyOnce); //QOS 2
                //await client.PublishAsync(new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes($"{message}")), MqttQualityOfService.ExactlyOnce); //QOS 2
                OnMessageReceived();
            }

            catch (Exception ex)
            {
                testLabel2.Text = "Ausnahme gefunden: " + ex.Message;
            }
        }

        ///////////////////////////////////////////////
        private void OnMessageReceived()
        {
                MessagingCenter.Subscribe<IMqttClient>(topic, "Hallooo", (sender) =>
                {
                    testLabel2.Text = "Du hast eine Nachricht bekommen";
                });
        }

0 Answers
Related