Skip to content

Commit

Permalink
fixed bug 863, support sets the MQTT client extended options, usage: …
Browse files Browse the repository at this point in the history
…mqttAdaptor := mqtt.NewAdaptorWithAuth(...); mqttAdaptor.SetOptionsFn(func(opts *paho.ClientOptions) {opts.WillPayload = []byte("goodbye")
  • Loading branch information
[email protected] committed Mar 27, 2024
1 parent ed0c79a commit f352e12
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion platforms/mqtt/mqtt_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit f352e12

Please sign in to comment.