MQTT协议之订阅及发布(使用paho-mqtt-client或mqttv3实现)
来源:互联网 发布:网络维护基础知识 编辑:程序博客网 时间:2024/06/06 04:25
另外一个MQTT发布订阅客户端paho-mqtt-client或mqttv3采用回调的方式实现消息的接收,下面看一下实现:
1.消息接收回调类
- package cn.smartslim.mqtt.demo.paho;
-
- import org.eclipse.paho.client.mqttv3.MqttCallback;
- import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
- import org.eclipse.paho.client.mqttv3.MqttMessage;
- import org.eclipse.paho.client.mqttv3.MqttTopic;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class PushCallback implements MqttCallback {
-
- public void connectionLost(Throwable cause) {
-
- System.out.println("连接断开,可以做重连");
- }
-
- public void deliveryComplete(MqttDeliveryToken token) {
-
- System.out.println("deliveryComplete---------"+ token.isComplete());
- }
-
- public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception {
-
- System.out.println("接收消息主题:"+topic.getName());
- System.out.println("接收消息Qos:"+message.getQos());
- System.out.println("接收消息内容:"+new String(message.getPayload()));
- }
-
- }
2.服务端发布消息
- package cn.smartslim.mqtt.demo.paho;
-
- import org.eclipse.paho.client.mqttv3.MqttClient;
- import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
- import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
- import org.eclipse.paho.client.mqttv3.MqttException;
- import org.eclipse.paho.client.mqttv3.MqttMessage;
- import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
- import org.eclipse.paho.client.mqttv3.MqttTopic;
- import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;
-
- public class Server {
-
- public static final String HOST = "tcp://192.168.36.102:1883";
-
- public static final String TOPIC = "tokudu/yzq124";
- private static final String clientid ="server";
-
- private MqttClient client;
- private MqttTopic topic;
- private String userName = "test";
- private String passWord = "test";
-
- private MqttMessage message;
-
- public Server() throws MqttException {
-
- client = new MqttClient(HOST, clientid, new MemoryPersistence());
- connect();
- }
-
- private void connect() {
- MqttConnectOptions options = new MqttConnectOptions();
- options.setCleanSession(false);
- options.setUserName(userName);
- options.setPassword(passWord.toCharArray());
-
- options.setConnectionTimeout(10);
-
- options.setKeepAliveInterval(20);
- try {
- client.setCallback(new PushCallback());
- client.connect(options);
- topic = client.getTopic(TOPIC);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void publish(MqttMessage message) throws MqttPersistenceException, MqttException{
- MqttDeliveryToken token = topic.publish(message);
- token.waitForCompletion();
- System.out.println(token.isComplete()+"========");
- }
-
- public static void main(String[] args) throws MqttException {
- Server server = new Server();
- server.message = new MqttMessage();
- server.message.setQos(1);
- server.message.setRetained(true);
- server.message.setPayload("eeeeeaaaaaawwwwww---".getBytes());
- server.publish(server.message);
- System.out.println(server.message.isRetained()+"------ratained状态");
- }
-
- }
3.客户端接收消息- package cn.smartslim.mqtt.demo.paho;
-
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
-
- 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.MqttSecurityException;
- import org.eclipse.paho.client.mqttv3.MqttTopic;
- import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;
-
- public class Client {
-
- public static final String HOST = "tcp://192.168.36.102:1883";
- public static final String TOPIC = "tokudu/yzq124";
- private static final String clientid = "client";
- private MqttClient client;
- private MqttConnectOptions options;
- private String userName = "test";
- private String passWord = "test";
-
- private ScheduledExecutorService scheduler;
-
-
- public void startReconnect() {
- scheduler = Executors.newSingleThreadScheduledExecutor();
- scheduler.scheduleAtFixedRate(new Runnable() {
- public void run() {
- if (!client.isConnected()) {
- try {
- client.connect(options);
- } catch (MqttSecurityException e) {
- e.printStackTrace();
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- }
- }, 0 * 1000, 10 * 1000, TimeUnit.MILLISECONDS);
- }
-
- private void start() {
- try {
-
- client = new MqttClient(HOST, clientid, new MemoryPersistence());
-
- options = new MqttConnectOptions();
-
- options.setCleanSession(true);
-
- options.setUserName(userName);
-
- options.setPassword(passWord.toCharArray());
-
- options.setConnectionTimeout(10);
-
- options.setKeepAliveInterval(20);
-
- client.setCallback(new PushCallback());
- MqttTopic topic = client.getTopic(TOPIC);
-
- options.setWill(topic, "close".getBytes(), 0, true);
-
- client.connect(options);
-
- int[] Qos = {1};
- String[] topic1 = {TOPIC};
- client.subscribe(topic1, Qos);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void disconnect() {
- try {
- client.disconnect();
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
-
-
-
- public static void main(String[] args) throws MqttException {
- Client client = new Client();
- client.start();
- }
-
- }
Android手机客户端实现demo:
https://github.com/dobermai/android-mqtt-push