Skip to content

Commit

Permalink
Mqtt: allow secure connection using client options (#2025)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Dec 11, 2021
1 parent 553bf56 commit 8323f2f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions provider/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand All @@ -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())
}
Expand Down

0 comments on commit 8323f2f

Please sign in to comment.