|
@@ -15,6 +15,7 @@ import org.springframework.boot.ApplicationRunner;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -47,8 +48,6 @@ public class MqttClientUtil implements ApplicationRunner {
|
|
|
options.setMaxReconnectDelay(128000);
|
|
|
// 允许的最大传输中消息
|
|
|
options.setMaxInflight(100);
|
|
|
- // 设置“遗嘱”消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息。
|
|
|
- options.setWill("willTopic", "offline".getBytes(), 0, false);
|
|
|
|
|
|
StringBuffer url = new StringBuffer();
|
|
|
url.append("tcp://").append(configMqtt.getIp()).append(":").append(configMqtt.getHost());
|
|
@@ -94,7 +93,13 @@ public class MqttClientUtil implements ApplicationRunner {
|
|
|
// 允许的最大传输中消息
|
|
|
options.setMaxInflight(100);
|
|
|
// 设置“遗嘱”消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息。
|
|
|
- options.setWill("willTopic", "offline".getBytes(), 0, false);
|
|
|
+ //获取所有主题
|
|
|
+ String[] topics = configMqtt.getTopic().split(",");
|
|
|
+ if(topics!=null&&topics.length!=0){
|
|
|
+ for (String topic : topics) {
|
|
|
+ options.setWill(Arrays.toString(topics), "offline".getBytes(), 2, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
//创建连接对象
|
|
|
client = new MqttClient(url.toString(), configMqtt.getId(), new MemoryPersistence());
|
|
|
//设置回调对象
|
|
@@ -278,7 +283,7 @@ public class MqttClientUtil implements ApplicationRunner {
|
|
|
// 允许的最大传输中消息
|
|
|
options.setMaxInflight(100);
|
|
|
// 设置“遗嘱”消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息。
|
|
|
- options.setWill("willTopic", "offline".getBytes(), 0, false);
|
|
|
+ options.setWill("willTopic", "offline".getBytes(), 2, false);
|
|
|
|
|
|
StringBuffer url = new StringBuffer();
|
|
|
url.append("tcp://").append(configMqtt.getIp()).append(":").append(configMqtt.getHost());
|