1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00

41 lines
702 B
Go
Raw Normal View History

2020-05-07 09:34:09 +02:00
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mqtt
import (
"errors"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
const (
username = "mainflux-mqtt"
qos = 2
2020-05-07 09:34:09 +02:00
)
var errConnect = errors.New("failed to connect to MQTT broker")
func newClient(address string, timeout time.Duration) (mqtt.Client, error) {
opts := mqtt.NewClientOptions().
SetUsername(username).
AddBroker(address)
2020-05-07 09:34:09 +02:00
client := mqtt.NewClient(opts)
token := client.Connect()
if token.Error() != nil {
return nil, token.Error()
}
ok := token.WaitTimeout(timeout)
if !ok {
return nil, errConnect
}
if token.Error() != nil {
return nil, token.Error()
}
2020-05-07 09:34:09 +02:00
return client, nil
}