【发布时间】:2017-05-03 23:02:17
【问题描述】:
我正在尝试使用paho pkg 通过 golang 构建 mqtt 子客户端, 当代理断开连接时我的客户端出现问题,我认为应该丢失消息appear,但这不会发生,如果我启动代理, mqtt子客户端收不到mqtt pub客户端发送的消息。
为什么会发生这种情况,我该如何解决?
代码
package main
import (
"fmt"
"os"
mqtt "github.com/eclipse/paho.mqtt.golang"
)
var (
broker = "tcp://localhost:1883"
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() {
//create a ClientOptions
opts := mqtt.NewClientOptions().AddBroker(broker)
opts.SetClientID("group-one")
opts.SetDefaultPublishHandler(f)
//create and start a client using the above ClientOptions
c := mqtt.NewClient(opts)
if token := c.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
if token := c.Subscribe("test", 0, nil); token.Wait() && token.Error() != nil {
fmt.Println(token.Error())
os.Exit(1)
}
for {
}
}
【问题讨论】: