C# MQTT客户端--M2MQTT

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxpsxp12/article/details/77870358

m2mqtt库:https://github.com/eclipse/paho.mqtt.m2mqtt
m2mqtt手册:https://m2mqtt.wordpress.com/using-mqttclient/
m2mqtt下载:https://www.nuget.org/packages/M2Mqtt/

准备

  • 本客户端使用VS2015进行开发,所以需要实现安装VS2015。
  • 开发之前需要下载M2MQTT库。m2mqtt库的获取,可以使用vs的nuget来获取。
  • 如果VS2015没有NuGet包管理器,可以按照https://jingyan.baidu.com/article/066074d636fd19c3c21cb0a2.html安装。
  • 重启VS2015后,打开自己的C#工程,然后打开“工具”-“Nuget包管理器”-“程序包管理器控制台”
    在控制台中输入“Install-Package M2Mqtt -Version 4.3.0”即可自动完成配置。

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Net;
using System.Security.Cryptography.X509Certificates;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using System.Net.Security;

namespace m2mqtt_test
{
    class Program
    {
        static void messageReceive(object sender, MqttMsgPublishEventArgs e)
        {
            string msg = "Topic:" + e.Topic + "   Message:" + System.Text.Encoding.Default.GetString(e.Message);
            Console.WriteLine(msg);
        }
        static bool cafileValidCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            string msg = "X509 链状态:";
            foreach (X509ChainStatus status in chain.ChainStatus)
            {
                msg += status.StatusInformation + "\n";
            }
            msg += "SSL策略问题:" + (int)sslPolicyErrors;

            Console.WriteLine(msg);

            if (sslPolicyErrors != SslPolicyErrors.None)
                return false;
            return true;
        }

        static void Main(string[] args)
        {
            string brokerHostName = "192.168.1.38";
            int brokerPort = 8883;
            string clientId = "m2mqtt";
            string username = "develop";
            string password = "666666";
            string[] topic = {"topic"};
            byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE};
            string caPath = "C:/MqttSSL/ca.crt";
            X509Certificate caCert = new X509Certificate(caPath);
            Console.WriteLine(caCert.ToString(true)+"\n"+caCert.ToString());
            Console.ReadKey();
            Console.WriteLine("------------------------分割线-------------------------------");
            //无SSL连接
            //MqttClient client = new MqttClient(brokerHostName,brokerPort,false,null,null,MqttSslProtocols.None);
            try {
                //单向SSL通信
                MqttClient client = new MqttClient(brokerHostName, brokerPort, true, caCert, null, MqttSslProtocols.TLSv1_2, new RemoteCertificateValidationCallback(cafileValidCallback));
                //消息接受
                client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(messageReceive);
                //连接Broker
                client.Connect(clientId, username, password);

                client.Subscribe(topic, qosLevels);
            }
            catch (System.Exception)
            {
                Console.WriteLine("连接失败!");
                Console.ReadKey();
            }

        }
    }
}

阅读更多
换一批

没有更多推荐了,返回首页