From 8323f2f100800265d6bc9c492cd3f992db9da415 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 11 Dec 2021 13:28:35 +0100 Subject: [PATCH] Mqtt: allow secure connection using client options (#2025) --- provider/mqtt/client.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/provider/mqtt/client.go b/provider/mqtt/client.go index 555e1ac6a..45b290de2 100644 --- a/provider/mqtt/client.go +++ b/provider/mqtt/client.go @@ -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()) }