Mqtt: allow secure connection using client options (#2025)
This commit is contained in:
parent
553bf56d9f
commit
8323f2f100
1 changed files with 8 additions and 2 deletions
|
|
@ -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())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue