上一篇寫了安裝mosQuitto和測試,可是用cmd命令很麻煩,有沒有一個可視化軟件呢? 有,須要在google瀏覽器下載一個叫MQTTLens的插件java
打開MQTTLens後界面以下:瀏覽器
打開connections後面的加號,就能夠鏈接安全
綠色,說明鏈接成功,在Subscribe下添加訂閱的topic名稱(這裏就寫topic了) 點擊subscribe 完成訂閱服務器
網上找了個java的測試代碼,進行鏈接測試session
pom.xml 引入包:eclipse
<dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.0.2</version> </dependency>
package mqtt; import java.util.concurrent.ScheduledExecutorService; 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.MqttTopic; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; public class ClientMQTT { public static final String HOST = "tcp://localhost:1883"; public static final String TOPIC = "topic"; private static final String clientid = "client11"; private MqttClient client; private MqttConnectOptions options; private String userName = "admin"; private String passWord = "admin"; private ScheduledExecutorService scheduler; private void start() { try { // host爲主機名,clientid即鏈接MQTT的客戶端ID,通常以惟一標識符表示,MemoryPersistence設置clientid的保存形式,默認爲之內存保存 client = new MqttClient(HOST, clientid, new MemoryPersistence()); // MQTT的鏈接設置 options = new MqttConnectOptions(); // 設置是否清空session,這裏若是設置爲false表示服務器會保留客戶端的鏈接記錄,這裏設置爲true表示每次鏈接到服務器都以新的身份鏈接 options.setCleanSession(true); // 設置鏈接的用戶名 options.setUserName(userName); // 設置鏈接的密碼 options.setPassword(passWord.toCharArray()); // 設置超時時間 單位爲秒 options.setConnectionTimeout(10); // 設置會話心跳時間 單位爲秒 服務器會每隔1.5*20秒的時間向客戶端發送個消息判斷客戶端是否在線,但這個方法並無重連的機制 options.setKeepAliveInterval(20); // 設置回調 client.setCallback(new PushCallback()); MqttTopic topic = client.getTopic(TOPIC); // setWill方法,若是項目中須要知道客戶端是否掉線能夠調用該方法。設置最終端口的通知消息 options.setWill(topic, "close".getBytes(), 2, true); client.connect(options); // 訂閱消息 int[] Qos = { 1 }; String[] topic1 = { TOPIC }; client.subscribe(topic1, Qos); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws MqttException { ClientMQTT client = new ClientMQTT(); client.start(); } }
package mqtt; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttMessage; /** * 發佈消息的回調類 * * 必須實現MqttCallback的接口並實現對應的相關接口方法CallBack 類將實現 MqttCallBack。 * 每一個客戶機標識都須要一個回調實例。在此示例中,構造函數傳遞客戶機標識以另存爲實例數據。 在回調中,將它用來標識已經啓動了該回調的哪一個實例。 * 必須在回調類中實現三個方法: * * public void messageArrived(MqttTopic topic, MqttMessage message)接收已經預訂的發佈。 * * public void connectionLost(Throwable cause)在斷開鏈接時調用。 * * public void deliveryComplete(MqttDeliveryToken token)) 接收到已經發布的 QoS 1 或 QoS 2 * 消息的傳遞令牌時調用。 由 MqttClient.connect 激活此回調。 * */ public class PushCallback implements MqttCallback { public void connectionLost(Throwable cause) { // 鏈接丟失後,通常在這裏面進行重連 System.out.println("鏈接斷開,能夠作重連"); } 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())); } }
package mqtt; import java.io.UnsupportedEncodingException; 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.persist.MemoryPersistence; /** * Title:Server Description: 服務器向多個客戶端推送主題,即不一樣客戶端可向服務器訂閱相同主題 */ public class ServerMQTT { // tcp://MQTT安裝的服務器地址:MQTT定義的端口號 public static final String HOST = "tcp://192.168.10.80:1883"; // 定義一個主題 public static final String TOPIC = "topic"; // 定義MQTT的ID,能夠在MQTT服務配置中指定 private static final String clientid = "server11"; private static MqttClient client; private static MqttTopic topic11; //用戶名和密碼 private String userName = "admin"; private String passWord = "admin"; private MqttMessage message; /** * 構造函數 * * @throws MqttException */ public ServerMQTT() throws MqttException { // MemoryPersistence設置clientid的保存形式,默認爲之內存保存 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); topic11 = client.getTopic(TOPIC); } catch (Exception e) { e.printStackTrace(); } } /** * * @param topic * @param message * @throws MqttPersistenceException * @throws MqttException */ public void publish(MqttTopic topic, MqttMessage message) throws MqttPersistenceException, MqttException { MqttDeliveryToken token = topic.publish(message); token.waitForCompletion(); System.out.println("message is published completely! " + token.isComplete()); } /** * 啓動入口 * * @param args * @throws MqttException * @throws UnsupportedEncodingException */ public static void main(String[] args) throws MqttException, UnsupportedEncodingException { ServerMQTT server = new ServerMQTT(); server.message = new MqttMessage(); /* Qos服務質量等級: * 0,最多一次,無論是否接收到; * 1,最少一次,保證信息將會被至少發送一次給接受者 * 2,只一次,確保每一個消息都只被接收到的一次,他是最安全也是最慢的服務等級 */ server.message.setQos(2); server.message.setRetained(true); server.message.setPayload("test".getBytes("UTF-8")); server.publish(server.topic11, server.message); System.out.println(server.message.isRetained() + "------ratained狀態"); } }
運行ServerMQTT中的main方法 發送test給mqtt,在MQTTlens中看到了接收的消息,說明推送成功tcp