Go 如何检查是否与mqtt代理失去连接?

Go 如何检查是否与mqtt代理失去连接?,go,mqtt,paho,Go,Mqtt,Paho,我正在尝试与pkg一起由golang构建mqtt子客户端, 当代理断开连接时,我与我的客户有问题,我认为应该会丢失消息,但这不会发生,如果我启动代理, mqtt子客户端无法获取mqtt pub客户端发送的消息 为什么会发生这种情况,我如何解决 代码 分配一个自定义的OnConnectionLossHandler来捕获连接丢失事件,以便在客户端失去连接时执行附加操作。如果将AutoReconnect选项设置为true(这是默认行为),则客户端将在连接丢失后自动重新连接到代理。请注意,连接丢失后,代

我正在尝试与pkg一起由golang构建mqtt子客户端, 当代理断开连接时,我与我的客户有问题,我认为应该会丢失消息,但这不会发生,如果我启动代理, mqtt子客户端无法获取mqtt pub客户端发送的消息

为什么会发生这种情况,我如何解决

代码


分配一个自定义的
OnConnectionLossHandler
来捕获连接丢失事件,以便在客户端失去连接时执行附加操作。如果将
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)
    }
}

谢谢你们的回答,但我想默认情况下必须通知我我失去了连接!!!默认处理程序只打印消息(我的坏消息,上面的示例也只打印消息…)。我的意思是,如果您指定一个自定义连接丢失处理程序,您可以做的不仅仅是打印消息,例如,计算连接丢失的数量,通过电子邮件发送通知等。
onconnectionlosshandler
如果我断开连接,则不打印任何内容。要查看消息,您需要设置
DEBUG
处理程序,例如,将
mqtt.DEBUG=log.New(os.Stderr,“DEBUG”,log.Ltime)
添加到
main
函数的第一行。这应该是paho的默认示例—在断开连接事件后必须重新订阅的事实非常令人惊讶。
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)
    }
}