提交 08fb230d 编写于 作者: 浅梦2013's avatar 浅梦2013

代码优化

上级 420af542
......@@ -49,7 +49,7 @@ public class MqttServerTest {
// mqtt 3.1 协议会校验 clientId 长度。
// .maxClientIdLength(64)
.messageListener((context, clientId, topic, qos, message) -> {
logger.info("clientId:{} message:{} payload:{}", clientId, message, ByteBufferUtil.toString(message.getPayload()));
logger.info("clientId:{} payload:{}", clientId, ByteBufferUtil.toString(message.getPayload()));
})
// 客户端连接状态监听
.connectStatusListener(new MqttConnectStatusListener())
......
......@@ -172,9 +172,16 @@ public final class TopicUtil {
* @return 获取处理完成之后的 topic
*/
public static String getTopicFilter(String topicTemplate) {
// 替换 ${name} 为 + 替换 #{name} 为 #
return topicTemplate.replaceAll("\\$\\{[\\s\\w.]+}", "+")
.replaceAll("#\\{[\\s\\w.]+}", "#");
// 替换 ${name} 为 +
StringBuilder sb = new StringBuilder(topicTemplate.length());
int cursor = 0;
for (int start, end; (start = topicTemplate.indexOf("${", cursor)) != -1 && (end = topicTemplate.indexOf('}', start)) != -1; ) {
sb.append(topicTemplate, cursor, start);
sb.append('+');
cursor = end + 1;
}
sb.append(topicTemplate.substring(cursor));
return sb.toString();
}
}
......@@ -61,4 +61,12 @@ class TopicUtilTest {
Assertions.assertTrue(TopicUtil.match("/iot/test/123", "/iot/test/123"));
}
@Test
void test2() {
String s1 = "$SYS/brokers/${node}/clients/${clientId}/disconnected";
String s2 = "$SYS/brokers/+/clients/+/disconnected";
String s3 = TopicUtil.getTopicFilter(s1);
Assertions.assertEquals(s3, s2);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册