forked from antlinker/libmqtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
134 lines (121 loc) · 3.79 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright Go-IIoT (https://github.com/goiiot)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package examples
import (
"log"
"time"
"github.com/goiiot/libmqtt"
)
// ExampleClient example of client creation
func ExampleClient() {
client, err := libmqtt.NewClient(
// try MQTT 5.0 and fallback to MQTT 3.1.1
libmqtt.WithVersion(libmqtt.V5, true),
// server address(es)
libmqtt.WithServer("localhost:1883"),
// enable keepalive (10s interval) with 20% tolerance
libmqtt.WithKeepalive(10, 1.2),
// enable auto reconnect and set backoff strategy
libmqtt.WithAutoReconnect(true),
libmqtt.WithBackoffStrategy(time.Second, 5*time.Second, 1.2),
// use RegexRouter for topic routing if not specified
// will use TextRouter, which will match full text
libmqtt.WithRouter(libmqtt.NewRegexRouter()),
)
if err != nil {
// handle client creation error
panic("hmm, how could it failed")
}
// register handlers
{
// register net handler
client.HandleNet(func(server string, err error) {
if err != nil {
log.Printf("error happened to connection to server [%v]: %v", server, err)
}
})
// register persist handler, you don't need this if all your message had QoS 0
client.HandlePersist(func(err error) {
if err != nil {
log.Printf("session persist error: %v", err)
}
})
// register subscribe handler
client.HandleSub(func(topics []*libmqtt.Topic, err error) {
if err != nil {
for _, t := range topics {
log.Printf("subscribe to topic [%v] failed: %v", t.Name, err)
}
} else {
for _, t := range topics {
log.Printf("subscribe to topic [%v] success: %v", t.Name, err)
}
// publish some packet (just for example)
client.Publish([]*libmqtt.PublishPacket{
{TopicName: "foo", Payload: []byte("bar"), Qos: libmqtt.Qos0},
{TopicName: "bar", Payload: []byte("foo"), Qos: libmqtt.Qos1},
}...)
}
})
// register unsubscribe handler
client.HandleUnSub(func(topic []string, err error) {
if err != nil {
// handle unsubscribe failure
for _, t := range topic {
log.Printf("unsubscribe to topic [%v] failed: %v", t, err)
}
} else {
for _, t := range topic {
log.Printf("unsubscribe to topic [%v] failed: %v", t, err)
}
}
})
// register publish handler
client.HandlePub(func(topic string, err error) {
if err != nil {
log.Printf("publish packet to topic [%v] failed: %v", topic, err)
} else {
log.Printf("publish packet to topic [%v] success: %v", topic, err)
}
})
// handle every subscribed message (just for example)
client.Handle(".*", func(topic string, qos libmqtt.QosLevel, msg []byte) {
log.Printf("[%v] message: %v", topic, string(msg))
})
}
// connect to server
client.Connect(func(server string, code byte, err error) {
if err != nil {
log.Printf("connect to server [%v] failed: %v", server, err)
return
}
if code != libmqtt.CodeSuccess {
log.Printf("connect to server [%v] failed with server code [%v]", server, code)
return
}
// connected
go func() {
// subscribe to some topics
client.Subscribe([]*libmqtt.Topic{
{Name: "foo", Qos: libmqtt.Qos0},
{Name: "bar", Qos: libmqtt.Qos1},
}...)
// in this example, we publish packets right after subscribe succeeded
// see `client.HandleSub`
}()
})
client.Wait()
}