Skip to content

Commit 5620c5e

Browse files
committed
notifications
1 parent 45048cc commit 5620c5e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

cmd/simple/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ func main() {
3939
opts.SetKeepAlive(2 * time.Second)
4040
opts.SetDefaultPublishHandler(f)
4141
opts.SetPingTimeout(1 * time.Second)
42+
opts.SetConnectionNotificationHandler(func(client mqtt.Client, notification mqtt.ConnectionNotification) {
43+
switch n := notification.(type) {
44+
case mqtt.ConnectionNotificationConnected:
45+
fmt.Printf("[NOTIFICATION] connected\n")
46+
case mqtt.ConnectionNotificationConnecting:
47+
fmt.Printf("[NOTIFICATION] connecting (isReconnect=%t) [%d]\n", n.IsReconnect, n.Attempt)
48+
case mqtt.ConnectionNotificationFailed:
49+
fmt.Printf("[NOTIFICATION] connection failed: %v\n", n.Reason)
50+
case mqtt.ConnectionNotificationLost:
51+
fmt.Printf("[NOTIFICATION] connection lost: %v\n", n.Reason)
52+
case mqtt.ConnectionNotificationBroker:
53+
fmt.Printf("[NOTIFICATION] broker connection: %s\n", n.Broker.String())
54+
case mqtt.ConnectionNotificationBrokerFailed:
55+
fmt.Printf("[NOTIFICATION] broker connection failed: %v [%s]\n", n.Reason, n.Broker.String())
56+
}
57+
})
4258

4359
c := mqtt.NewClient(opts)
4460
if token := c.Connect(); token.Wait() && token.Error() != nil {

0 commit comments

Comments
 (0)