转:EMQTT测试--安装与测试 (windows)

官网

我下载的是windows版

安装

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

将下载的压缩包解压,我解压到了D盘

这里写图片描述

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

控制台模式启动:

.inemqttd console

报错如下

无法启动此程序,因为计算机中丢失 MSVCR120.dll。尝试重新安装该程序以解决次问题。

这里写图片描述

找一个MSVCR120.dll文件,可以去网上下载,也可以在自己电脑上找

这里写图片描述

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

我拷贝一个mqttfx下的到WindowsSystem32下

这里写图片描述

然后重新执行

.inemqttd 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 

原文地址:https://www.cnblogs.com/saryli/p/9739272.html