1. 程式人生 > >轉:EMQTT測試--安裝與測試 (windows)

轉:EMQTT測試--安裝與測試 (windows)

pen bsp option util sage block exc sta clientid

官網

我下載的是windows版

安裝

參考
http://emqtt.com/docs/install.html

將下載的壓縮包解壓,我解壓到了D盤

技術分享圖片

命令行窗口,cd到程序目錄

控制臺模式啟動:

.\bin\emqttd console

報錯如下

無法啟動此程序,因為計算機中丟失 MSVCR120.dll。嘗試重新安裝該程序以解決次問題。

技術分享圖片

找一個MSVCR120.dll文件,可以去網上下載,也可以在自己電腦上找

技術分享圖片

HBuilder下有
mqttfx下也有
XMind下也有
IDEA下也有

我拷貝一個mqttfx下的到Windows\System32下

技術分享圖片

然後重新執行

.\bin\emqttd console

啟動成功

技術分享圖片

登錄

http://localhost:18083/

用戶名為admin密碼為public

技術分享圖片

登錄上去就可以看服務器的運行狀態了

技術分享圖片

測試

依賴

    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>1.0.2</version>
    </dependency>

  

Server

package com.mymqtt.myemqtt;

import java.util.Scanner;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;

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);
        }

    }
}

  

Client

package com.mymqtt.myemqtt;

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();
    }
}

  

PushCallback

package com.mymqtt.myemqtt;

import java.util.Date;

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;

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(message);
        System.out.println("接收消息主題 : " + topic);
        System.out.println("接收消息Qos : " + message.getQos());
        System.out.println("接收消息內容 : " + new String(message.getPayload()));
    }

}

結果

技術分享圖片

控制臺顯示

兩個連接

技術分享圖片

參考文獻

http://emqtt.com/

百度開放雲物接入IoT–Java客戶端

--------------------- 本文來自 GW_Cheng 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/frankcheng5143/article/details/52045501?utm_source=copy

轉:EMQTT測試--安裝與測試 (windows)