C#Mqtt订阅消息

物联网数据通讯,需要从服务器上的mqtt服务器上订阅数据,

xml格式如下:

<?xml version="1.0" encoding="utf-8"?>
<Order>
  <Items>
    <Mqtt Ip="121.43.168.148"   Port="6000" Name="Data0" password="0"/>
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data0" startid="0" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data1" startid="1" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data2" startid="2" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data3" startid="3" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data4" startid="4" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data5" startid="5" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data6" startid="6" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data7" startid="7" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data8" startid="8" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data9" startid="9" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data10" startid="10" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data11" startid="11" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data12" startid="12" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data13" startid="13" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data14" startid="14" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data15" startid="15" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data16" startid="16" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data17" startid="17" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data18" startid="18" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data19" startid="19" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data20" startid="20" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data21" startid="21" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data22" startid="22" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data23" startid="23" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data24" startid="24" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data25" startid="25" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data26" startid="26" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data27" startid="27" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data28" startid="28" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data29" startid="29" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data30" startid="30" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data31" startid="31" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data32" startid="32" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data33" startid="33" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data34" startid="34" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data35" startid="35" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data36" startid="36" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data37" startid="37" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data38" startid="38" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data39" startid="39" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data40" startid="40" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data41" startid="41" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data42" startid="42" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data43" startid="43" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data44" startid="44" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data45" startid="45" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data46" startid="46" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data47" startid="47" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data48" startid="48" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data49" startid="49" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data50" startid="50" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data51" startid="51" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data52" startid="52" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data53" startid="53" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data54" startid="54" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data55" startid="55" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data56" startid="56" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data57" startid="57" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data58" startid="58" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data59" startid="59" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data60" startid="60" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data61" startid="61" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data62" startid="62" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data63" startid="63" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data64" startid="64" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data65" startid="65" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data66" startid="66" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data67" startid="67" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data68" startid="68" numid="10" note="10" Value="10" />
    <OrderItem Room="1#配电房" Line="进线柜" Name="Data69" startid="69" numid="10" note="10" Value="10" />

  </Items>
</Order>

订阅主题为1#配电房/进线柜/Data0等

下图为订阅到部分数据

订阅代码:

 string topic = "/home/tempurture/line1";

            MqttClient client = new MqttClient(serverIp, Convert.ToInt32(serverPort), false, null, null, MqttSslProtocols.TLSv1_2);
            string clientId = Guid.NewGuid().ToString();
            string username = "admin";
            string password = "password";
            //建立连接
            client.Connect(clientId, username, password);
            for (int m = 0; m < xml.datalist.Count; m++)
            {
                topic = xml.datalist[m];
                // 订阅主题 "/home/temperature", 订阅质量 QoS 2 
                client.Subscribe(new string[] { topic }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
            }
            label1.Text = topic;
            client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(messageReceive);
            timer1.Interval = Convert.ToInt32(textStartId.Text) * 1000 + 30000;
            this.timer1.Enabled = true;

 需要源码的发我邮箱,279644337@qq.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值