首页 > 解决方案 > Golang 中 mqtt 客户端的零星断开连接

问题描述

我在 GO 中使用 paho mqtt 客户端连接到代理。一段时间内一切正常,我可以在其中发布到主题并收听任何订阅。过了一会儿,客户端失去了与代理的连接,这个日志没有明显的原因:

[MQTT] ERROR: 2018/09/04 17:31:30 [net] outgoing stopped with error write tcp IP1 -> IP2: write: broken pipe
[MQTT] ERROR: 2018/09/04 17:31:30 [net] logic received from error channel, other components have errored, stopping

我正在像这样连接到经纪人:

opts := mqtt.NewClientOptions().
    AddBroker(broker).
    SetClientID(fmt.Sprintf("%s-%v", ProgramName, time.Now().Unix())).
    SetUsername(username).
    SetPassword(password).
    SetOrderMatters(true)
if tlsConfig != nil {
    opts.SetTLSConfig(tlsConfig)
}

pahoClient := mqtt.NewClient(opts)
client = &DefaultMQTTClient{
    c:            pahoClient,
    subscribeQoS: subscribeQoS,
    publishQoS:   publishQoS,
    name:         name,
    log: Log.WithPrefix("mqtt").WithFields(logrus.Fields{
        "name":   name,
        "broker": broker,
    }),
}

client.Log().Debug("connecting to mqtt broker")
if err = client.Connect(); err != nil {
    err = client.Log().Trace(err, "failed to connect to mqtt broker")
    return
}
client.Log().Info("established connection with mqtt broker")

我使用的 paho 版本是 1.0.0。欢迎任何提示!

标签: gomqttpaho

解决方案


推荐阅读