Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
z2m
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xAAL
Code
Go
z2m
Commits
f57b637f
Commit
f57b637f
authored
2 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Handle reconnecting after MQTT broker is down
parent
127fe665
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mqtt.go
+23
-6
23 additions, 6 deletions
mqtt.go
with
23 additions
and
6 deletions
mqtt.go
+
23
−
6
View file @
f57b637f
...
...
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log/slog"
"time"
mqtt
"github.com/eclipse/paho.mqtt.golang"
)
...
...
@@ -14,19 +15,35 @@ func MQTTSetup(cfg *Config, publishHandler mqtt.MessageHandler) mqtt.Client {
opts
:=
mqtt
.
NewClientOptions
()
.
AddBroker
(
fmt
.
Sprintf
(
"tcp://%s:%d"
,
cfg
.
brokerHost
,
cfg
.
brokerPort
))
.
SetClientID
(
mqttClientID
)
.
SetDefaultPublishHandler
(
publishHandler
)
.
SetAutoReconnect
(
true
)
SetDefaultPublishHandler
(
publishHandler
)
.
SetConnectionLostHandler
(
MQTTLostCnx
)
.
SetMaxReconnectInterval
(
time
.
Minute
*
2
)
opts
=
opts
.
SetOnConnectHandler
(
func
(
client
mqtt
.
Client
)
{
MQTTSubscribe
(
client
,
cfg
)
})
client
:=
mqtt
.
NewClient
(
opts
)
if
token
:=
client
.
Connect
();
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
panic
(
token
.
Error
())
}
slog
.
Debug
(
"Connected to"
,
"Broker"
,
cfg
.
brokerHost
,
"Port"
,
cfg
.
brokerPort
,
"Client"
,
mqttClientID
)
if
token
:=
client
.
Subscribe
(
cfg
.
topic
+
"/#"
,
0
,
nil
);
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
panic
(
token
.
Error
())
// panic(token.Error())
slog
.
Warn
(
"MQTTSetup failed"
,
"err"
,
token
.
Error
())
}
return
client
}
func
MQTTSubscribe
(
client
mqtt
.
Client
,
cfg
*
Config
)
error
{
token
:=
client
.
Subscribe
(
cfg
.
topic
+
"/#"
,
0
,
nil
)
if
err
:=
token
.
Wait
();
token
.
Error
()
!=
nil
{
return
fmt
.
Errorf
(
"failed to subscribe to topic '%s/#': %v"
,
cfg
.
topic
,
err
)
}
slog
.
Debug
(
"Subscribed to"
,
"Topic"
,
cfg
.
topic
+
"/#"
)
return
nil
}
func
MQTTLostCnx
(
client
mqtt
.
Client
,
err
error
)
{
slog
.
Warn
(
"MQTT connection lost"
)
}
// MQTTDumpMsg displays the MQTT message
func
MQTTDumpMsg
(
msg
mqtt
.
Message
)
{
var
data
map
[
string
]
interface
{}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment