【发布时间】:2015-12-21 04:24:35
【问题描述】:
开始在 .net 环境中使用 MQTT,作为一个新手,遇到一些问题,例如无法在发布者和订阅者之间进行通信。从 eclipse.org 我得到下面的语法
// SUBSCRIBER
...
// create client instance
MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(new string[] { "/home/temperature" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
...
static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// handle message received
}
// PUBLISHER
...
// create client instance
MqttClient client = new MqttClient(IPAddress.Parse(MQTT_BROKER_ADDRESS));
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
string strValue = Convert.ToString(value);
// publish a message on "/home/temperature" topic with QoS 2
client.Publish("/home/temperature", Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE);
...
在上面的语法中,我是如何获得 MQTT_BROKER_ADDRESS 的,到目前为止,我从 hivemq 等其他文档中了解到我需要部署它。有没有经纪人可以使用它?我需要 MQTT for .net 上的 HELLO WORLD 类型的项目。 其中有发布者、订阅者和代理。
【问题讨论】:
标签: c# .net eclipse mqtt mosquitto