如何使用Emqttd

1.下载安装

进入如下网站: 

    http://emqtt.com/downloads 进行下载

下面以安装Windows版为例子:

点击红框中的链接,进行下载.


2.下载复制到D:盘,进行解压 到emqttd目录,

2.1在cmd模式下, 进入D:\emqttd\bin目录:执行如下命令:emqttd start 

2.2:执行完成后,可以在浏览器中,打开如下网站: http://127.0.0.1:18083 可以打开什么安装成功

默认用户名:admin  密码:public


2.3.登入成功,可以进入管理页面:



2.使用手册(详细使用说明,请看官方文档)

官方文档网站:http://emqtt.com/docs/v2/index.html



3.C#客户端开发方法

3.1下面就是使用M2MQTT的库进行简单开发:

在Visual studio中新建一个项目 :MQTT 

引用M2MQTT包:



3.2加入下面的代码:

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

        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值