mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* correct suscriber interface validator + refactore token error handling Signed-off-by: tzzed <zerouali.t@gmail.com> * apply review suggestion Signed-off-by: tzzed <zerouali.t@gmail.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
42 lines
721 B
Go
42 lines
721 B
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package mqtt
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
)
|
|
|
|
const (
|
|
protocol = "mqtt"
|
|
username = "mainflux-mqtt"
|
|
qos = 2
|
|
)
|
|
|
|
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)
|
|
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()
|
|
}
|
|
|
|
return client, nil
|
|
}
|