Skip to content

Commit

Permalink
✨ mica-mqtt client stater MqttClientTemplate 完善,统一调整客户端示例。
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Aug 26, 2022
1 parent f1533ce commit ec1c8ae
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### v1.3.9 - 2022-08-28
- :sparkles: mica-mqtt server 添加消息拦截器,gitee #I5KLST
- :sparkles: mica-mqtt client、server ack 优化和完善,可自定义 ackService。
- :sparkles: mica-mqtt client stater MqttClientTemplate 完善,统一调整客户端示例。
- :sparkles: mica-mqtt client 优化客户端心跳和心跳日志优化。
- :sparkles: mica-mqtt client 订阅代码优化。
- :sparkles: mica-mqtt codec 代码优化。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ClientService {
private MqttClientTemplate client;

public boolean publish(String body) {
client.publish("/test/client", ByteBuffer.wrap(body.getBytes(StandardCharsets.UTF_8)));
client.publish("/test/client", body.getBytes(StandardCharsets.UTF_8));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import net.dreamlu.iot.mqtt.core.client.MqttClient;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Timer;
import java.util.TimerTask;
Expand All @@ -43,7 +42,7 @@ public static void main(String[] args) {
timer.schedule(new TimerTask() {
@Override
public void run() {
client.publish("/a/door/open", ByteBuffer.wrap("open".getBytes(StandardCharsets.UTF_8)));
client.publish("/a/door/open", "open".getBytes(StandardCharsets.UTF_8));
}
}, 5000, 5000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
Expand Down Expand Up @@ -50,6 +49,6 @@ public static void main(String[] args) {
client.unSubscribe("/test/#", "/test/123");

// 连接上之后发送消息,注意:连接时出现异常等就不会发出
client.publish("/test/client", ByteBuffer.wrap("mica最牛皮".getBytes(StandardCharsets.UTF_8)));
client.publish("/test/client", "mica最牛皮".getBytes(StandardCharsets.UTF_8));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,42 @@ public MqttClient unSubscribe(List<String> topicFilters) {
return client.unSubscribe(topicFilters);
}

/**
* 发布消息
*
* @param topic topic
* @param payload 消息内容
* @return 是否发送成功
*/
public boolean publish(String topic, byte[] payload) {
return client.publish(topic, payload, MqttQoS.AT_MOST_ONCE);
}

/**
* 发布消息
*
* @param topic topic
* @param payload 消息内容
* @param qos MqttQoS
* @return 是否发送成功
*/
public boolean publish(String topic, byte[] payload, MqttQoS qos) {
return client.publish(topic, payload, qos, false);
}

/**
* 发布消息
*
* @param topic topic
* @param payload 消息体
* @param qos MqttQoS
* @param retain 是否在服务器上保留消息
* @return 是否发送成功
*/
public boolean publish(String topic, byte[] payload, MqttQoS qos, boolean retain) {
return client.publish(topic, payload, qos, retain);
}

/**
* 发布消息
*
Expand Down

0 comments on commit ec1c8ae

Please sign in to comment.