# MQTT Go 客户端库
Eclipse Paho MQTT Go Client (opens new window) 为 Eclipse Paho 项目下的 Go 语言版客户端库,该库能够连接到 MQTT Broker 以发布消息,订阅主题并接收已发布的消息,支持完全异步的操作模式。
客户端依赖于 Google 的 proxy (opens new window) 和 websockets (opens new window) 软件包,通过以下命令完成安装:
go get github.com/eclipse/paho.mqtt.golang
Copied!
1
# MQTT Go 使用示例
本示例包含 Go 语言的 Paho MQTT 连接 EMQX Broker,并进行消息收发完整代码:
package main import ( "fmt" "log" "os" "time" "github.com/eclipse/paho.mqtt.golang" ) var f mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { fmt.Printf("TOPIC: %s\n", msg.Topic()) fmt.Printf("MSG: %s\n", msg.Payload()) } func main() { mqtt.DEBUG = log.New(os.Stdout, "", 0) mqtt.ERROR = log.New(os.Stdout, "", 0) opts := mqtt.NewClientOptions().AddBroker("tcp://broker.emqx.io:1883").SetClientID("emqx_test_client") opts.SetKeepAlive(60 * time.Second) // 设置消息回调处理函数 opts.SetDefaultPublishHandler(f) opts.SetPingTimeout(1 * time.Second) c := mqtt.NewClient(opts) if token := c.Connect(); token.Wait() && token.Error() != nil { panic(token.Error()) } // 订阅主题 if token := c.Subscribe("testtopic/#", 0, nil); token.Wait() && token.Error() != nil { fmt.Println(token.Error()) os.Exit(1) } // 发布消息 token := c.Publish("testtopic/1", 0, false, "Hello World") token.Wait() time.Sleep(6 * time.Second) // 取消订阅 if token := c.Unsubscribe("testtopic/#"); token.Wait() && token.Error() != nil { fmt.Println(token.Error()) os.Exit(1) } // 断开连接 c.Disconnect(250) time.Sleep(1 * time.Second) }
Copied!
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
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
# Paho Golang MQTT 5.0 支持
MQTT 5.0 支持参考 eclipse/paho.golang (opens new window)