Skip to content

Commit

Permalink
feat(mqtt): improve connection handling
Browse files Browse the repository at this point in the history
  • Loading branch information
adutchak committed Apr 9, 2024
1 parent aebb027 commit 1e4ff5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ func main() {
continue
}

if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
l.Error("Error connecting to MQTT", token.Error())
err = mqttclient.ConnectToMqtt(mqttClient)
if err != nil {
l.Error("Failed to connect to MQTT", err)
}
defer mqttClient.Disconnect(250)

Expand Down
7 changes: 7 additions & 0 deletions pkg/mqttclient/mqttclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ func GetMqttClient(configuration *config.Config) (mqtt.Client, error) {
}
return client, nil
}

func ConnectToMqtt(client mqtt.Client) error {
if token := client.Connect(); token.Wait() && token.Error() != nil {
return token.Error()
}
return nil
}

0 comments on commit 1e4ff5b

Please sign in to comment.