当前位置: 首页 > news >正文

连接HiveMQ代理器实现MQTT协议传输

先下载MQTTX: MQTTX: Your All-in-one MQTT Client Toolbox

使用线上免费的MQTTX BROKER:The Free Global Public MQTT Broker | Try Now | EMQ

打开MQTTX,创建连接,点击NEW SUBSCRIPTION,创建一个主题,这里使用test/topic,在下面Json中填写配置好的主题,点击发送测试OK。订阅者,建立一个主题:

package com.jasonhong.application.media.mq.mqtt;import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;  
import org.eclipse.paho.client.mqttv3.MqttClient;  
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;  
import org.eclipse.paho.client.mqttv3.MqttException;  
import org.eclipse.paho.client.mqttv3.MqttMessage;  
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;  public class MqttSubscriber implements MqttCallback {  public static void main(String[] args) {  String brokerUrl = "tcp://broker.hivemq.com:1883"; // 使用公共MQTT代理或你的MQTT代理地址
//        String brokerUrl = "tcp://localhost:1883"; // Mosquitto代理地址和端口String clientId = "JavaSubscriber";  String topic = "test/topic";  int qos = 2;  try (MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence())) {  client.setCallback(new MqttSubscriber());  MqttConnectOptions connOpts = new MqttConnectOptions();  connOpts.setCleanSession(true);  client.connect(connOpts);  client.subscribe(topic, qos);  } catch (MqttException e) {  e.printStackTrace();  }  }  @Override  public void connectionLost(Throwable cause) {  System.out.println("Connection lost");  cause.printStackTrace();  }  @Override  public void messageArrived(String topic, MqttMessage message) throws Exception {  System.out.println("Message arrived: " + new String(message.getPayload()));  }  @Override  public void deliveryComplete(IMqttDeliveryToken token) {  System.out.println("Delivery complete");  }  
}

发布者发送给一个消息

package com.jasonhong.application.media.mq.mqtt;import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;  
import org.eclipse.paho.client.mqttv3.MqttException;  
import org.eclipse.paho.client.mqttv3.MqttMessage;  
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;  public class MqttPublisher {  public static void main(String[] args) {  String brokerUrl = "tcp://broker.hivemq.com:1883"; // 使用公共MQTT代理或你的MQTT代理地址
//        String brokerUrl = "tcp://localhost:1883"; // Mosquitto代理地址和端口String clientId = "JavaPublisher";  String topic = "test/topic";  int qos = 2;  String content = "Hello, MQTT!";  try (MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence())) {  MqttConnectOptions connOpts = new MqttConnectOptions();  connOpts.setCleanSession(true);  client.connect(connOpts);  MqttMessage message = new MqttMessage(content.getBytes());  message.setQos(qos);  client.publish(topic, message);  System.out.println("Message published: " + content);  } catch (MqttException e) {  e.printStackTrace();  }  }  
}
http://www.lryc.cn/news/343803.html

相关文章:

  • springcloud报错:Failed to start bean‘webServerStartStop‘
  • el-checkbox 无法动态设置勾选状态
  • 车规级低功耗汽车用晶振SG-9101CGA
  • 企业是保留传统的MES还是换新的MES?
  • 2024年第六届世界软件工程研讨会(WSSE 2024)即将召开!
  • Linux网络编程:TCP编程实现
  • 小剧场短剧影视小程序源码_后端PHP
  • C语言总结三:数组(压缩版)
  • 我独自升级崛起怎么玩 我独自升级崛起游玩教程分享
  • 前端上传大文件
  • Kompas AI图片转换器:高效解决格式不兼容问题
  • 自动驾驶规划与控制技术解析
  • 计算机等级考试常见问题
  • C语言实战项目--贪吃蛇
  • 【LAMMPS学习】八、基础知识(5.3)Body particles体粒子
  • 【3D目标检测】常见相关指标说明
  • QT设计模式:工厂模式
  • 【电路笔记】-容抗
  • 基于若依框架搭建网站的开发日志(一):若依框架搭建、启动、部署
  • Android中Fragment失去焦点的场景
  • Linux变量的认识及环境变量配置详解
  • 【excel】数据非数值导致排序失效
  • 软件网关--Nginx
  • sourceTree push失败
  • leetCode33. 搜索旋转排序数组
  • JS_监听dom变化触发,new MutationObserver
  • 什么是驱动数字签名?如何获取驱动数字签名?
  • 【leetcode】优先队列题目总结
  • typescript 中的泛型
  • 计算方法实验2(补充):列主元消元法解线性方程组