MQTT协议(Message Queuing Telemetry Transport),翻译过来就是遥信消息队列传输,是IBM公司于1999年提出的,如今最新版本是3.1.1。MQTT是一个基于TCP的发布订阅协议,设计的初始目的是为了极有限的内存设备和网络带宽很低的网络不可靠的通讯,很是适合物联网通讯html
特色:前端
1轻量,java
2能够在网络差的环境中使用 算法
3 使用发布订阅模式chrome
4遗嘱apache
5消息有三种QOS(消息质量)(0只发布无论关心不收到,1最少收到一次,2确保收到一次,只一次)浏览器
愈来愈的的物联网,互联网应用在使用Mqtt协议,小黄车就是其中之一 。能够被用做即时通信,消息队列,消息推送(智联招聘企业版就使用mqtt作前端的消息推送)等等。各类语言的成熟库都有。安全
总之就是很棒,应用很广,未来会更广。服务器
Net也不少库网络
咱们这里使用MQTTnet
using MQTTnet; using MQTTnet.Client.Connecting; using MQTTnet.Client.Disconnecting; using MQTTnet.Client.Options; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace AliyunMqtt2 { class Program { static void Main(string[] args) { string CId = "1"; //用户标识ID String userName = "admin"; //用户名 String passWord = "password"; //密码 var will = new MqttApplicationMessage() { Topic = "lastwill", Payload = System.Text.Encoding.UTF8.GetBytes("我掉线了") };//定义遗嘱消息 IMqttClientOptions Option = new MqttClientOptionsBuilder().WithTcpServer("127.0.0.1", 61613)//地址端口号 .WithClientId(CId) //客户端标识Id要惟一。 .WithCredentials(userName, passWord) //用户名,密码 .WithWillMessage(will) //加上遗嘱消息 .WithCleanSession() .Build(); MqttFactory factory = new MqttFactory(); var mqttClient = factory.CreateMqttClient(); //建立客户端实例 mqttClient.Connected += (object sender, MqttClientConnectedEventArgs e) => //链接成功 { Console.WriteLine("链接成功:" + CId); var topics = new List<TopicFilter>(); topics.Add(new TopicFilter() { Topic = "家具/#" }); mqttClient.SubscribeAsync(topics);//订阅 Console.WriteLine("订阅成功:"); mqttClient.PublishAsync("家具/饮水机/加热/10度", "10");//发布 Console.WriteLine("发布成功"); }; mqttClient.Disconnected += (object sender, MqttClientDisconnectedEventArgs e) => { Console.WriteLine("断开链接:" + CId); }; mqttClient.ApplicationMessageReceived += (object sender, MqttApplicationMessageReceivedEventArgs e) => { ///收到消息 string content = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload); Console.WriteLine($"收到消息 msg={content}:"); }; mqttClient.ConnectAsync(Option); Console.ReadLine(); } } }
Mqtt须要一个Broker服务器作消息的中转站,全部的客户端发布都是往broker发布,订阅也都是从broker订阅
这里咱们介绍两种broker,免费简单的apollo和稳定高性能的阿里云mqtt服务
1.下载地址 http://activemq.apache.org/apollo/download.html
2.建立一个broker实例,命令行cd到bin目录,执行/bin/apollo create mybroker,执行后就会在bin目录下建立mybroker文件夹。
3.运行broker实例,命令行cd到mybroker/bin目录,执行mybroker/bin/apollo-broker.cmd run
注:apollo依赖java环境。
运行成功的界面
4在浏览器查看链接的情况
在浏览器输入
http://127.0.0.1:61680/ 或者 https://127.0.0.1:61681/,默认帐号 admin,密码 password
在这里看当前链接情况
1,首先要上阿里云购买服务
购买地址https://common-buy.aliyun.com/?commodityCode=onsMqtt#/buy
购买成功进入控制台
要是看不到实例列表的话,注意是否选择对了区域,默认是公网,我这里是华南区
选择group管理
建立一个新的groupid
而后就可使用应用程序链接了
经常使用的net mqtt库有MQTTnet,M2Mqtt我这里使用 MQTTnet
使用Nuget安装
此处的username和pwd须要从阿里云帐户里面获取
在右上角获取
这个分别就是key和secret
而后链接mqtt的时候username用key
Pwd用groupid加secret的哈希算法得来
public static string HMACSHA1(string key, string dataToSign)
{
Byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(key);
HMACSHA1 hmac = new HMACSHA1(secretBytes);
Byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(dataToSign);
Byte[] calcHash = hmac.ComputeHash(dataBytes);
String calcHashString = Convert.ToBase64String(calcHash);
return calcHashString;
}
链接地址
在阿里云的控制台获取 即接入点域名 ,这里就再也不须要端口号了
链接成功
添加MqttLens
使用谷歌浏览器 打开谷歌插架商店搜索MqttLens 并添加到浏览器
添加成功后在浏览器打开chrome://apps/
点击运行添加一个链接
测试发布订阅
测试成功
遗嘱是mqtt的一个大特色
遗嘱的原理:链接成功后当即每每broker发送一条消息(遗嘱),但不生效,当客户端断开链接,broker检测不到心跳时,遗嘱生效。
消息格式应当是下功夫去定义的,好的topic格式可让咱们更简洁方便
三种符号
/ 表明层级 通常定义消息时都以 /分割成层级
好比 家具/饮水机/加热
通配符+ 表明一层匹配
好比
好比有设备 饮水机想要订阅 加热和保温两个topic,只须要订阅家具/饮水机/+
全通配符 # 能彻底匹配,不限制层数
Apollo 免费 数量少没问题,数量大就不是很稳定了。并且实测时链接数和机器的内核数有关好比我这里4核的 WinServer最多只能连上512个
EMQ 国产的。也有免费版。各类服务器的版本都有,也很不错,
这里是官网 http://www.emqtt.com/
阿里云微消息服务若是可靠性要求比较高,或者客户端的数量比较大时,推荐使用阿里云。价格也不是很贵,安全性高,稳定性很高,这里仍是很推荐。