前言

MQTT协议是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分,http://mqtt.org/。

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios. It is also ideal for mobile applications because of its small size, low power usage, minimised data packets, and efficient distribution of information to one or many receivers

通过https://github.com/mqtt/mqtt.github.io/wiki/servers 找到官方推荐的服务端软件,比如:Apache Apollo,

通过https://github.com/mqtt/mqtt.github.io/wiki/libraries可以找到推荐的客户端类库,比如:Eclipse Paho Java

MQTTnet

MQTTnet 是MQTT协议的.NET 开源类库。

MQTTnet is a .NET library for MQTT based communication. It provides a MQTT client and a MQTT server. The implementation is based on the documentation from http://mqtt.org/.

通过Nuget搜索MQTT找到了MQTTnet,它不是下载量最多的,也不在官方推荐列表中,主要是因为同时支持客户端和服务端,所以开始下载试用,结果证明有坑,源码在vs2015中不能打开,客户端示例接收不到消息。

开源地址:https://github.com/chkr1011/MQTTnet

首先把官方的控制台程序改成winform的,界面如下:

 public partial class Form1 : Form{private MqttServer mqttServer = null;private MqttClient mqttClient = null;public Form1(){InitializeComponent();}private void button_启动服务端_Click(object sender, EventArgs e){MqttTrace.TraceMessagePublished += MqttTrace_TraceMessagePublished;if (this.mqttServer == null){try{var options = new MqttServerOptions{ConnectionValidator = p =>{if (p.ClientId == "SpecialClient"){if (p.Username != "USER" || p.Password != "PASS"){return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;}}return MqttConnectReturnCode.ConnectionAccepted;}};mqttServer = new MqttServerFactory().CreateMqttServer(options);}catch (Exception ex){MessageBox.Show(ex.Message);return;}}mqttServer.Start();this.txt_服务器.AppendText( $">> 启动成功..." + Environment.NewLine);}private void MqttTrace_TraceMessagePublished(object sender, MqttTraceMessagePublishedEventArgs e){this.Invoke(new Action(() =>{this.txt_服务器.AppendText($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}" + Environment.NewLine);if (e.Exception != null){this.txt_服务器.AppendText( e.Exception + Environment.NewLine);}}));}private void button_停止服务端_Click(object sender, EventArgs e){if (mqttServer != null){mqttServer.Stop();}this.txt_服务器.AppendText( $">> 停止成功" + Environment.NewLine);}private async void button_启动客户端_Click(object sender, EventArgs e){if (this.mqttClient == null){var options = new MqttClientOptions{Server = "192.168.2.54",ClientId = "zbl",CleanSession = true};this.mqttClient = new MqttClientFactory().CreateMqttClient(options);this.mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived;this.mqttClient.Connected += MqttClient_Connected;this.mqttClient.Disconnected += MqttClient_Disconnected;}try{await this.mqttClient.ConnectAsync();}catch(Exception ex){this.txt_客户端.AppendText( $"### CONNECTING FAILED ###" + Environment.NewLine);}}private void MqttClient_Connected(object sender, EventArgs e){this.Invoke(new Action( async () =>{this.txt_客户端.AppendText( $"### CONNECTED WITH SERVER ###" + Environment.NewLine);await this.mqttClient.SubscribeAsync(new List<TopicFilter>{new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)});this.txt_客户端.AppendText($"### SUBSCRIBED  ###" + Environment.NewLine);}));}private void MqttClient_Disconnected(object sender, EventArgs e){this.Invoke(new Action(() =>{this.txt_客户端.AppendText( $"### DISCONNECTED FROM SERVER ###" + Environment.NewLine);}));}private void MqttClient_ApplicationMessageReceived(object sender, MQTTnet.Core.MqttApplicationMessageReceivedEventArgs e){this.Invoke(new Action(() =>{this.txt_客户端.AppendText( $">> Topic:{e.ApplicationMessage.Topic} Payload:{Encoding.UTF8.GetString(e.ApplicationMessage.Payload)} QoS:{e.ApplicationMessage.QualityOfServiceLevel}   Retain:{e.ApplicationMessage.Retain}" + Environment.NewLine);}));}private async void button_停止客户端_Click(object sender, EventArgs e){if (this.mqttClient != null){await this.mqttClient.DisconnectAsync();}this.txt_客户端.AppendText( $">> 停止成功" + Environment.NewLine);}private async void button_发送_Click(object sender, EventArgs e){//var options = new MqttClientOptions//{//    Server = "localhost"//};//var client = new MqttClientFactory().CreateMqttClient(options);//await client.ConnectAsync();var applicationMessage = new MqttApplicationMessage(this.txt_topic.Text,Encoding.UTF8.GetBytes(this.txt_message.Text), MqttQualityOfServiceLevel.AtMostOnce, false);await this.mqttClient.PublishAsync(applicationMessage);}private void button_清空服务端_Click(object sender, EventArgs e){this.txt_服务器.Text = "";}}

需要注意的是按照作者说明是

1
2
3
4
5
6
7
8
9
10
11
12
13
while (true)
{
    Console.ReadLine();
    var applicationMessage = new MqttApplicationMessage(
        "A/B/C",
        Encoding.UTF8.GetBytes("Hello World"),
        MqttQualityOfServiceLevel.AtLeastOnce,
        false
    );
    await client.PublishAsync(applicationMessage);
}

 客户端死活都收不到消息,改成 MqttQualityOfServiceLevel.AtMostOnce  就可以了,找问题时尝试下载源码调试因为vs2015打不开项目也折腾了一会。这个问题提交了issues,期待作者回复。

Apache Apollo

1.下载Apollo服务器,我这里用的是Binaries for Windows。下载后解压到一个文件夹,注意路径不要包含中文,安装手册

2.创建Broker Instance,命令行cd到bin目录,执行/bin/apollo create mybroker,执行后就会在bin目录下创建mybroker文件夹。

3.运行Broker Instance,命令行cd到mybroker/bin目录,执行mybroker/bin/apollo-broker.cmd run

4.Web Administrator,地址 http://127.0.0.1:61680/ or https://127.0.0.1:61681/,默认账号 admin,密码 password

参考

  • MQTT协议的简单介绍和服务器的安装,
  • 编写和MQTT服务器通信的Android客户端程序,
  • MQTT】在Windows下搭建MQTT服务器

搭建了MQTT服务端之后需要在Esp8266模块和手机App中分别实现客户端功能,稍后待续。。。。

分类: IoT

转载于:https://www.cnblogs.com/webenh/p/9133792.html

快速搭建MQTT服务器(MQTTnet和Apache Apollo)相关推荐

  1. nodejs快速搭建MQTT服务器

    简介 MQTT(消息队列遥测传输)是ISO 标准(ISO/IEC PRF 20922)下基于发布/订阅范式的消息协议.它工作在TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而 ...

  2. 转 【MQTT】在Windows下搭建MQTT服务器

    MQTT简介 MQ 遥测传输 (MQTT) 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放.简单.轻量.易于实现.这些特点使它适用于受限环境.该协议的特点有: 使用发布/订阅消息模式,提供 ...

  3. 【MQTT】在Windows下搭建MQTT服务器

    MQTT简介 MQ 遥测传输 (MQTT) 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放.简单.轻量.易于实现.这些特点使它适用于受限环境.该协议的特点有: 使用发布/订阅消息模式,提供 ...

  4. linux如何搭建mqtt服务器,【MQTT】在Ubuntu下搭建MQTT服务器

    前言 博主之前写了一篇<在Windows下搭建MQTT服务器>,这次要尝试在Ubuntu下搭建MQTT服务器.实际上,下载好源码包后,后面的都和那篇文章差不多了. 开发环境 虚拟机 Ubu ...

  5. 阿里云服务器ECS-Apollo搭建MQTT服务器(Windows环境)

    阿里云服务器(Windows环境)Apollo搭建MQTT服务器 1.购买阿里云服务器ECS 注册阿里云账号– 学生通过认证可免费领取2个月,由于网上教程很多,这里不做阐述了,我是领取的windows ...

  6. 【MQTT从入门到提高系列 | 01】从0到1快速搭建MQTT测试环境

    这是机器未来的第24篇文章 原文首发地址:https://blog.csdn.net/RobotFutures/article/details/125532208 1. mosquitto概述 Ecl ...

  7. Windows搭建MQTT服务器

    Win10下搭建MQTT服务器 一.获取软件包 链接:https://pan.baidu.com/s/1sKsL3ninhpwiawm69cOQ9w 提取码:9vhm mqtt服务器安装包为apach ...

  8. 手把手搭建企业IT实战环境第三季:快速搭建SCCM1902服务器

    手把手搭建企业IT实战环境第三季:快速搭建SCCM1902服务器 ©Lander Zhang 专注外企按需IT基础架构运维服务,IT Helpdesk 实战培训践行者 博客:https://blog. ...

  9. MQTT在Windows下搭建MQTT服务器

    MQTT 在Windows下搭建MQTT服务器 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012163234/article/details/ ...

最新文章

  1. redis集群之REDIS CLUSTER
  2. 深入DataGrid分页样式实例
  3. 数据结构-挖坑填数+分治法解决快速排序问题(java+c)
  4. 同步和异步GET,POST请求
  5. 永冻土层matlab图片,北极圈都32℃了!千年永冻土层快“热化”了?
  6. 现在多少钱能和以80年代的万元户持平?
  7. 软件设计师 - 超键、无损连接、函数依赖
  8. SpringSecurity Exceptions
  9. Faker 快速构造测试数据
  10. python游戏开发库_Python库之游戏开发及虚拟现实
  11. 苹果无人车四个最新专利:手势控制变道、车辆导流、路况感知及车辆控制
  12. bootstrap datetimepicker日期插件使用方法
  13. java抓取豆瓣网页内容_爬取豆瓣网页上的电影(包括图片,评分,和简介等)
  14. 勤哲excel服务器端口协议,勤哲Excel服务器技术支持|Excel服务器常见问题解答
  15. adobe flash player ActiveX IE降级安装旧版本的方法
  16. 一直以来很喜欢的NewAge纯音乐
  17. 内网穿透-Frp(1)使用樱花Frp(Sakura Frp)进行免费的内网穿透操作步骤
  18. 场效应管及其放大电路
  19. linux i2c smbus驱动
  20. 艾伟:一次挂死(hang)的处理过程及经验

热门文章

  1. 基于日志的交换机故障预测
  2. SpringCloud Consul注册中心介绍及配置使用
  3. java多态的好处_java萌新,对象的多态有什么好处?
  4. Jetty 与 Tomcat
  5. 一直处于building “XXX”gradle project info
  6. PHP排雷之编码问题
  7. volatile是怎么保证可见性和有序性的,为什么无法保证原子性
  8. 【MyBatis框架】高级映射-一对一查询
  9. Rust中Box、Rc、Arc、Cell、RefCell、Cow简介
  10. matlab gui表格行标1234没有,MATLAB GUI:滑塊作爲滾動條;子面板不夾