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()) }