https://blog.csdn.net/qq_35797735/article/details/115110546?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-9-115110546.pc_agg_new_rank&utm_term=emqtt%E4%BD%BF%E7%94%A8&spm=1000.2123.3001.4430
一、简述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级"通讯协议,该协议构建于TCP/IP协议上,由IBM在1999年发布。MQTT最大优点在于,可以以极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务。作为一种低开销、低带宽占用的即时通讯协议,使其在物联网、小型设备、移动应用等方面有较广泛的应用。 MQTT是一个基于客户端-服务器的消息发布/订阅传输协议(TCP)。MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。在很多情况下,包括受限的环境中,如:机器与机器(M2M)通信和物联网(IoT)。其在,通过卫星链路通信传感器、偶尔拨号的医疗设备、智能家居、及一些小型化设备中已广泛使用。 二、官网下载 官网地址:https://www.emqx.cn/ 下载流程:MQTT-->MQTT-->MQTT官网-->Software(软件)-->Servers/Brokers(服务/代理)-->[EMQ图标] EMQX-->Download Now(现在下载)-->中间顶部选for Cloud-->EMQ X Broker-->Windows(这里看你系统需要下载,我下的是Windows)--> 然后点击 emqx-windows-4.2.8.zip -->解压到你想放的盘符 三、启动服务 启动:dos 命令到你解压的盘符,比如我是放在F盘下面,F:--> cd emqx-->cd bin-->emqx start 停止:emqx stop 查看服务是否启动(我知道的方式有两种): 1、使用dos命令查看:F:--> cd emqx-->cd bin-->emqx status ,如出现以下提示则启动成功 Node 'emqx@127.0.0.1' is started emqx 4.2.8 is running 2、浏览器地址栏中直接输入:http://localhost:18083/ 用户名:admin 密码:public 登录成功页面:
<dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.2.2</version> </dependency>
服务端 package com.gdcy.qldlgf.core.mqtt; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttMessage; import java.util.Scanner; public class Server { public static void main(String[] args) throws Exception { String host = "tcp://127.0.0.1:1883"; String topic = "hello"; String clientId = "server";// clientId不能重复 MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(true); MqttClient client = new MqttClient(host, clientId); client.connect(options); MqttMessage message = new MqttMessage(); @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); System.out.println("请输入要发送的内容:"); while (true) { String line = scanner.nextLine(); message.setPayload(line.getBytes()); client.publish(topic, message); } } }
客户端 package com.gdcy.qldlgf.core.mqtt; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; public class Client { public static void main(String[] args) throws Exception { String host = "tcp://127.0.0.1:1883"; String topic = "hello"; String clientId = "12345";// clientId不能重复 // 1.设置mqtt连接属性 MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(true); // 2.实例化mqtt客户端 MqttClient client = new MqttClient(host, clientId); // 3.连接 client.connect(options); client.setCallback(new PushCallback()); while (true) { client.subscribe(topic, 2); } // client.disconnect(); } }
回调,类似监听,监听订阅的topic是否有新内容发过来 package com.gdcy.qldlgf.core.mqtt; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; public class PushCallback implements MqttCallback { private final Logger logger = LoggerFactory.getLogger(this.getClass()); public void connectionLost(Throwable cause) { // 连接丢失后,一般在这里面进行重连 System.out.println("连接断开,可以做重连"); logger.info("掉线时间:{}", new Date()); } public void deliveryComplete(IMqttDeliveryToken token) { System.out.println("deliveryComplete---------" + token.isComplete()); } public void messageArrived(String topic, MqttMessage message) throws Exception { // subscribe后得到的消息会执行到这里面 System.out.println("接收消息主题 : " + topic); System.out.println("接收消息Qos : " + message.getQos()); System.out.println("接收消息内容 : " + new String(message.getPayload())); } }
1、先启动Client类中main方法。
2、再启动server类中main 方法,发布主题内容,就可在client控制台中看见