C#应用程序使用mqtt代理

时间:2017-04-20 04:51:24

标签: c# mqtt

我在显示mqtt代理主题时遇到问题。我得到的只是System.byte[]我知道我连接的每次我从hiveMQ发布消息时,另一个System.bytes被添加到控制台。请帮忙。

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

}

1 个答案:

答案 0 :(得分:2)

你必须将byte []转换为字符串,以便在控制台中显示。

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