-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
62 lines (50 loc) · 1.51 KB
/
example_test.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
package goqtt_test
import (
"log"
"github.com/andschneider/goqtt"
)
// This example subscribes to a MQTT broker. You will want to call ReadLoop afterwards
// to process incoming messages.
func ExampleClient_Subscribe() {
// Create Client
client := goqtt.NewClient("mqtt.eclipse.org")
// Attempt a connection to the specified MQTT broker
client.Connect()
defer client.Disconnect()
// Attempt to subscribe to the topic
err := client.Subscribe()
if err != nil {
log.Fatalf("could not subscribe to topic: %v\n", err)
}
}
// This example subscribes to a MQTT broker will print any incoming messages to std out.
func ExampleClient_Publish() {
// Create Client
client := goqtt.NewClient("mqtt.eclipse.org")
// Attempt a connection to the specified MQTT broker
client.Connect()
defer client.Disconnect()
// Attempt to publish a message
err := client.Publish("hello world")
if err != nil {
log.Printf("could not send message: %v\n", err)
}
}
// This example subscribes to a MQTT broker will print any incoming messages to std out, until the program exits.
func ExampleClient_ReadLoop() {
// Create Client
client := goqtt.NewClient("mqtt.eclipse.org")
// Attempt a connection to the specified MQTT broker
client.Connect()
defer client.Disconnect()
// Attempt to subscribe to the topic
client.Subscribe()
// Read messages indefinitely
for {
log.Println("waiting for message")
m, _ := client.ReadLoop()
if m != nil {
log.Printf("received message: '%s' from topic: '%s'", string(m.Message), m.Topic)
}
}
}