From c6ea19470b390a04fee2a50551028ebb23a550a3 Mon Sep 17 00:00:00 2001 From: "shegaoyuan@aliyun.com" Date: Wed, 27 Mar 2024 20:00:15 +0800 Subject: [PATCH] fixed bug 683, support sets the MQTT client extended options, usage: mqttAdaptor := mqtt.NewAdaptorWithAuth(...); mqttAdaptor.SetOptionsFn(func(opts *paho.ClientOptions) {opts.WillPayload = []byte("goodbye") --- platforms/mqtt/mqtt_adaptor.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/platforms/mqtt/mqtt_adaptor.go b/platforms/mqtt/mqtt_adaptor.go index 3e93efe4..5f4f2a7d 100644 --- a/platforms/mqtt/mqtt_adaptor.go +++ b/platforms/mqtt/mqtt_adaptor.go @@ -32,6 +32,7 @@ type Adaptor struct { cleanSession bool client paho.Client qos int + optionsFn func(*paho.ClientOptions) } // NewAdaptor creates a new mqtt adaptor with specified host and client id @@ -109,9 +110,16 @@ func (a *Adaptor) ClientKey() string { return a.clientKey } // SetClientKey sets the MQTT client SSL key file func (a *Adaptor) SetClientKey(val string) { a.clientKey = val } +// SetOptionsFn sets the MQTT client extended options +func (a *Adaptor) SetOptionsFn(fn func(*paho.ClientOptions)) { a.optionsFn = fn } + // Connect returns true if connection to mqtt is established func (a *Adaptor) Connect() error { - a.client = paho.NewClient(a.createClientOptions()) + opts := a.createClientOptions() + if a.optionsFn != nil { + a.optionsFn(opts) + } + a.client = paho.NewClient(opts) if token := a.client.Connect(); token.Wait() && token.Error() != nil { return token.Error() }