|
@@ -35,9 +35,9 @@ type ClientConfig struct {
|
|
|
ClientID string //////////////////// 客户端的唯一标识
|
|
ClientID string //////////////////// 客户端的唯一标识
|
|
|
Username string //////////////////// 登录服务器的用户
|
|
Username string //////////////////// 登录服务器的用户
|
|
|
Password string //////////////////// 登录服务器的密码
|
|
Password string //////////////////// 登录服务器的密码
|
|
|
|
|
+ Qos byte //////////////////// 传输消息质量等级
|
|
|
WillTopic string //////////////////// 遗嘱消息发送主题
|
|
WillTopic string //////////////////// 遗嘱消息发送主题
|
|
|
WillPayload string //////////////////// 遗嘱消息发送内容
|
|
WillPayload string //////////////////// 遗嘱消息发送内容
|
|
|
- WillQos byte //////////////////// 遗嘱消息质量等级
|
|
|
|
|
WillRetained bool //////////////////// 遗嘱消息是否保留
|
|
WillRetained bool //////////////////// 遗嘱消息是否保留
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -46,6 +46,8 @@ type subscription struct {
|
|
|
handler mqtt.MessageHandler //////////// 订阅主题消息处理
|
|
handler mqtt.MessageHandler //////////// 订阅主题消息处理
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type MessageHandler func(client *Client, msg mqtt.Message)
|
|
|
|
|
+
|
|
|
type Client struct {
|
|
type Client struct {
|
|
|
client mqtt.Client
|
|
client mqtt.Client
|
|
|
config ClientConfig
|
|
config ClientConfig
|
|
@@ -77,11 +79,11 @@ func NewClient(config ClientConfig, ctx context.Context) (*Client, error) {
|
|
|
SetOrderMatters(false)
|
|
SetOrderMatters(false)
|
|
|
|
|
|
|
|
if config.WillTopic != "" { //////////////////////////////////////// 设置遗嘱消息 /////////
|
|
if config.WillTopic != "" { //////////////////////////////////////// 设置遗嘱消息 /////////
|
|
|
- if config.WillQos != MqttQos0 && config.WillQos != MqttQos1 && config.WillQos != MqttQos2 {
|
|
|
|
|
|
|
+ if config.Qos != MqttQos0 && config.Qos != MqttQos1 && config.Qos != MqttQos2 {
|
|
|
cancel()
|
|
cancel()
|
|
|
return nil, ErrInvalidQoS
|
|
return nil, ErrInvalidQoS
|
|
|
}
|
|
}
|
|
|
- opts = opts.SetWill(config.WillTopic, config.WillPayload, config.WillQos, config.WillRetained)
|
|
|
|
|
|
|
+ opts = opts.SetWill(config.WillTopic, config.WillPayload, config.Qos, config.WillRetained)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
opts.OnConnect = func(c mqtt.Client) {
|
|
opts.OnConnect = func(c mqtt.Client) {
|
|
@@ -264,7 +266,7 @@ func (c *Client) Publish(topic string, qos byte, retained bool, payload any) err
|
|
|
消息回调需支持并发调用,并尽快返回,尽量避免耗时或阻塞操作, 此类类任务应交由独立的goroutine或通
|
|
消息回调需支持并发调用,并尽快返回,尽量避免耗时或阻塞操作, 此类类任务应交由独立的goroutine或通
|
|
|
过消息队列来处理,避免阻塞mqtt底层的消息处理。
|
|
过消息队列来处理,避免阻塞mqtt底层的消息处理。
|
|
|
*/
|
|
*/
|
|
|
-func (c *Client) Subscribe(topic string, qos byte, handler mqtt.MessageHandler) error {
|
|
|
|
|
|
|
+func (c *Client) Subscribe(topic string, qos byte, handler MessageHandler) error {
|
|
|
if topic == "" {
|
|
if topic == "" {
|
|
|
return ErrTopicEmpty
|
|
return ErrTopicEmpty
|
|
|
}
|
|
}
|
|
@@ -283,12 +285,18 @@ func (c *Client) Subscribe(topic string, qos byte, handler mqtt.MessageHandler)
|
|
|
c.mu.Unlock()
|
|
c.mu.Unlock()
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
- c.subs[topic] = subscription{qos: qos, handler: handler} // 注意: 断线时也允许先登记订阅
|
|
|
|
|
|
|
+
|
|
|
|
|
+ wrappedHandler := func(_ mqtt.Client, msg mqtt.Message) {
|
|
|
|
|
+ handler(c, msg)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ c.subs[topic] = subscription{qos: qos, handler: wrappedHandler}
|
|
|
if !c.client.IsConnected() {
|
|
if !c.client.IsConnected() {
|
|
|
c.mu.Unlock()
|
|
c.mu.Unlock()
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
- token := c.client.Subscribe(topic, qos, handler)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ token := c.client.Subscribe(topic, qos, wrappedHandler)
|
|
|
c.mu.Unlock()
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
return c.waitToken(token)
|
|
return c.waitToken(token)
|