|
@@ -75,6 +75,7 @@ func NewClient(config ClientConfig, ctx context.Context) (*Client, error) {
|
|
|
SetUsername(config.Username).SetPassword(config.Password).SetClientID(config.ClientID).
|
|
SetUsername(config.Username).SetPassword(config.Password).SetClientID(config.ClientID).
|
|
|
SetConnectRetry(false).SetAutoReconnect(false).SetCleanSession(true).
|
|
SetConnectRetry(false).SetAutoReconnect(false).SetCleanSession(true).
|
|
|
SetKeepAlive(10 * time.Second).SetPingTimeout(5 * time.Second). // 保活超时&Ping心跳间隔
|
|
SetKeepAlive(10 * time.Second).SetPingTimeout(5 * time.Second). // 保活超时&Ping心跳间隔
|
|
|
|
|
+ // 下面的设置使得回调独立 goroutine 执行, 避免阻塞 MQTT 网络处理 /////////////////////////
|
|
|
SetOrderMatters(false)
|
|
SetOrderMatters(false)
|
|
|
|
|
|
|
|
if willTopic := config.Topics["Offline"]; willTopic != "" { // 设置遗嘱消息
|
|
if willTopic := config.Topics["Offline"]; willTopic != "" { // 设置遗嘱消息
|
|
@@ -285,10 +286,6 @@ func (c *Client) Publish(topic string, qos byte, retained bool, payload any) err
|
|
|
return c.waitToken(token)
|
|
return c.waitToken(token)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/*
|
|
|
|
|
-消息回调需支持并发调用,并尽快返回,尽量避免耗时或阻塞操作, 此类类任务应交由独立的goroutine或通
|
|
|
|
|
-过消息队列来处理,避免阻塞mqtt底层的消息处理。
|
|
|
|
|
-*/
|
|
|
|
|
func (c *Client) Subscribe(topic string, qos byte, handler MessageHandler) error {
|
|
func (c *Client) Subscribe(topic string, qos byte, handler MessageHandler) error {
|
|
|
if topic == "" {
|
|
if topic == "" {
|
|
|
return ErrTopicEmpty
|
|
return ErrTopicEmpty
|