-
依赖
-
Maven
先添加Jcenter仓库
<repository> <id>jcenter</id> <name>jcenter</name> <url>https://jcenter.bintray.com</url> </repository>
然后
<dependency> <groupId>com.dreamgyf.mqtt</groupId> <artifactId>gmqyttf-client</artifactId> <version>0.1.0</version> <type>pom</type> </dependency>
-
Gradle
先添加Jcenter仓库
repositories { jcenter() }
然后
implementation 'com.dreamgyf.mqtt:gmqyttf-client:0.1.0'
-
-
运行
//首先,创建一个MqttClient实例 MqttClient client = new MqttClient.Builder() .cleanSession(true) .clientId("test1") .usernameFlag(true) .passwordFlag(true) .username("dreamgyf") .password("dreamgyf") .keepAliveTime((short) 10) .willFlag(true) .willQoS(2) .willTopic("/willTopic") .willMessage("") .willRetain(true) .build(MqttVersion.V3_1_1); //设置回调 client.setCallback(new MqttClientCallback() { @Override public void onConnectSuccess() { System.out.println("Connection success"); } @Override public void onConnectionException(MqttException e) { e.printStackTrace(); System.err.println("Connection exception: " + e + " " + e.getMessage()); } @Override public void onSubscribeFailure(MqttTopic mqttTopic) { System.err.println("Subscription failed: " + mqttTopic.getTopic() + " QoS: " + mqttTopic.getQoS()); } @Override public void onMessageReceived(String topic, String message) { System.out.println("Message received, topic: " + topic + " message: " + message); } }); //连接,订阅和发布 client.connect("broker.emqx.io", 1883); client.subscribe(new MqttTopic("/dreamgyf/test", 2)); client.publish("/dreamgyf/test", "publish test", new MqttPublishOption().QoS(2));