【问题标题】:How I can check if I lose connection with mqtt broker?如何检查我是否与 mqtt 代理断开连接?
【发布时间】: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 {

    }
}

【问题讨论】:

    标签: go mqtt paho


    【解决方案1】:

    分配一个自定义OnConnectionLostHandler 来捕获连接丢失事件,这样您就可以在客户端丢失连接时执行其他操作。如果您将AutoReconnect 选项设置为true(这是默认行为),客户端将在连接丢失后自动重新连接到代理。请注意,连接断开后,您的订阅状态/信息可能不会被代理保存,因此您将无法收到任何消息。要处理此问题,请将主题订阅移至 OnConnect 处理程序。下面是一个示例实现:

    package main
    
    import (
        "fmt"
        "os"
        "time"
    
        mqtt "github.com/eclipse/paho.mqtt.golang"
    )
    
    func messageHandler(c mqtt.Client, msg mqtt.Message) {
        fmt.Printf("TOPIC: %s\n", msg.Topic())
        fmt.Printf("MSG: %s\n", msg.Payload())
    }
    
    func connLostHandler(c mqtt.Client, err error) {
        fmt.Printf("Connection lost, reason: %v\n", err)
    
        //Perform additional action...
    }
    
    func main() {
        //create a ClientOptions
        opts := mqtt.NewClientOptions().
            AddBroker("tcp://localhost:1883").
            SetClientID("group-one").
            SetDefaultPublishHandler(messageHandler).
            SetConnectionLostHandler(connLostHandler)
    
        //set OnConnect handler as anonymous function
        //after connected, subscribe to topic
        opts.OnConnect = func(c mqtt.Client) {
            fmt.Printf("Client connected, subscribing to: test/topic\n")
    
            //Subscribe here, otherwise after connection lost, 
            //you may not receive any message
            if token := c.Subscribe("test/topic", 0, nil); token.Wait() && token.Error() != nil {
                fmt.Println(token.Error())
                os.Exit(1)
            }
        }
    
        //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())
        }
    
        for {
            //Lazy...
            time.Sleep(500 * time.Millisecond)
        }
    }
    

    【讨论】:

    • 谢谢你的回答,但我认为OnConnectionLostHandler默认必须通知我我失去了连接!!!
    • 默认处理程序只打印消息(我的错,上面的示例也只打印消息......)。我的意思是,如果您分配一个自定义连接丢失处理程序,您可以做的不仅仅是打印一条消息,例如计算丢失的连接数,通过电子邮件发送通知等...
    • OnConnectionLostHandler 如果我失去连接,则不会打印任何东西。
    • 要查看消息,您需要设置DEBUG 处理程序,例如将mqtt.DEBUG = log.New(os.Stderr, "DEBUG ", log.Ltime) 添加到main 函数的第一行。
    • 这应该在 paho 的默认示例中 - 在断开连接事件后必须重新订阅这一事实非常令人惊讶。
    猜你喜欢
    • 2023-03-19
    • 2015-12-23
    • 2022-12-18
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多