Skip to content

Commit

Permalink
📝 梳理文档
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Feb 20, 2024
1 parent 37acaf6 commit fcb00a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) {
.username("admin")
.password("123456")
// 全局订阅的 topic
.globalSubscribe("/test", "/test/123")
.globalSubscribe("/test", "/test/123", "/debug/#")
// 全局监听,也会监听到服务端 http api 订阅的数据
.globalMessageListener((context, topic, message, payload) -> {
System.out.println("topic:\t" + topic);
Expand Down
20 changes: 20 additions & 0 deletions mica-mqtt-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ MqttClient client = MqttClient.create()
// 停止
client.stop();
```

## 全局订阅(2.2.9开始支持)
**说明**:由于 mica-mqtt-client 采用传统 mq 的思维进行的开发。其实是跟 mqtt 部分是有违背的。传统 mqtt client 不会按 topic 进行不通的订阅,采用的是这里的**全局订阅**方式。
**注意**:全局订阅也是可以监听到 `subQos0``subQos1``subQos2` 的消息。采用 `globalSubscribe`,保留 session 停机重启,依然可以接受到消息。
```java
// 初始化 mqtt 客户端
MqttClient.create()
.ip("127.0.0.1")
.port(1883)
.username("admin")
.password("123456")
// 全局订阅的 topic
.globalSubscribe("/test", "/test/123", "/debug/#")
// 全局监听,也会监听到服务端 http api 订阅的数据
.globalMessageListener((context, topic, message, payload) -> {
System.out.println("topic:\t" + topic);
System.out.println("payload:\t" + ByteBufferUtil.toString(payload));
})
.connectSync();
```
10 changes: 6 additions & 4 deletions starter/mica-mqtt-client-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ mqtt:
ip: 127.0.0.1 # 连接的服务端 ip ,默认:127.0.0.1
port: 1883 # 端口:默认:1883
name: Mica-Mqtt-Client # 名称,默认:Mica-Mqtt-Client
clientId: 000001 # 客户端Id(非常重要,一般为设备 sn,不可重复)
client-id: 000001 # 客户端Id(非常重要,一般为设备 sn,不可重复)
user-name: mica # 认证的用户名
password: 123456 # 认证的密码
global-subscribe: # 全局订阅的 topic,可被全局监听到,保留 session 停机重启,依然可以接受到消息。(2.2.9开始支持)
timeout: 5 # 超时时间,单位:秒,默认:5秒
reconnect: true # 是否重连,默认:true
re-interval: 5000 # 重连时间,默认 5000 毫秒
Expand Down Expand Up @@ -57,9 +58,10 @@ mqtt:
### 2.2 可实现接口(注册成 Spring Bean 即可)
| 接口 | 是否必须 | 说明 |
| --------------------------- |------| ------------------------- |
| IMqttClientConnectListener | 否 | 客户端连接成功监听 |
| 接口 | 是否必须 | 说明 |
| --------------------------- |------|--------------------------------|
| IMqttClientConnectListener | 否 | 客户端连接成功监听 |
| IMqttClientGlobalMessageListener | 否 | 全局消息监听,可以监听到所有订阅消息。(2.2.9开始支持) |
### 2.3 客户端上下线监听
使用 Spring event 解耦客户端上下线监听,注意: `1.3.4` 开始支持。会跟自定义的 `IMqttClientConnectListener` 实现冲突,取一即可。
Expand Down

0 comments on commit fcb00a1

Please sign in to comment.