Seth
Seth

Reputation: 15

C# application working with mqtt broker

I am having problem showing the topic from a mqtt broker. All I get is System.byte[] I know I'm connected as every time I publish a message from hiveMQ another System.bytes get added to the console. Please help.

static void Main(string[] args)
{
    MqttClient client = new MqttClient("myip", 1883,false,null,null,0,null,null);
    client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

    client.Connect(Guid.NewGuid().ToString());
    ushort msgId = client.Subscribe(new string[] { "broTest" },
    new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
    client.ProtocolVersion = MqttProtocolVersion.Version_3_1_1;
}

static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
    // handle message received

    Console.WriteLine("message=" + e.Message.ToString());

}

Upvotes: 0

Views: 4562

Answers (1)

jiten
jiten

Reputation: 5264

you have to convert byte[] to string, in order to show in console.

static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {               
            Console.WriteLine(Encoding.UTF8.GetString(e.Message));              
        }

Upvotes: 3

Related Questions