Mqtt: allow secure connection using client options (#2025)

This commit is contained in:
andig 2021-12-11 13:28:35 +01:00 • committed by GitHub
parent 553bf56d9f
commit 8323f2f100
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -48,11 +48,9 @@ type Option func(*paho.ClientOptions)
// NewClient creates new Mqtt publisher
func NewClient(log *util.Logger, broker, user, password, clientID string, qos byte, opts ...Option) (*Client, error) {
broker = util.DefaultPort(broker, 1883)
log.INFO.Printf("connecting %s at %s", clientID, broker)
mc := &Client{
log: log,
broker: broker,
Qos: qos,
listener: make(map[string][]func(string)),
}
@ -74,6 +72,14 @@ func NewClient(log *util.Logger, broker, user, password, clientID string, qos by
}
client := paho.NewClient(options)
or := client.OptionsReader()
mc.broker = fmt.Sprintf("%v", or.Servers())
if len(or.Servers()) == 1 {
mc.broker = or.Servers()[0].String()
}
log.INFO.Printf("connecting %s at %s", clientID, mc.broker)
if token := client.Connect(); token.Wait() && token.Error() != nil {
return nil, fmt.Errorf("error connecting: %w", token.Error())
}