【问题标题】:golang paho mqtt dropping messagesgolang paho mqtt 丢弃消息
【发布时间】:2021-07-29 15:30:00
【问题描述】:

我有一个 mosquitto 代理,它在防火墙后面的服务器中运行。所需的端口是开放的,我从外部检查它是否正在使用:

mosquitto_sub  -h [ip address] -t "#" -u [username] -P [password] -v


mosquitto_pub  -h [ip address] -t "topic" -u [username] -P [password] -m "hello"

我可以看到发布的消息。 我想用一个小的go程序做同样的事情,代码如下:

package main

import (
    "crypto/tls"
    "fmt"
    "time"
    "strconv"
    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)
}

func main() {

    opts := MQTT.NewClientOptions()

    skipVerify := true
    opts.AddBroker("tcp://[ip]:1883")
    opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: skipVerify})
    //opts.SetTLSConfig(&tls.Config{InsecureSkipVerify: skipVerify, ClientAuth: tls.NoClientCert})
    opts.SetClientID("myclientid")
    opts.SetAutoReconnect(true)
    //opts.SetCleanSession(true)
    opts.SetDefaultPublishHandler(messageHandler)
    opts.SetConnectionLostHandler(connLostHandler)

    opts.OnConnect = func(c MQTT.Client) {
        fmt.Printf("Client connected\n")
    }

    client := MQTT.NewClient(opts)
    token := client.Connect()
    token.Wait()
    fmt.Println("connected")
    if token.Error() != nil {
        fmt.Println("problems with connection")
        panic(token.Error())
    }
    for i := 0; i < 10; i++ {
        str := "hello: " + strconv.Itoa(i)
        token := client.Publish("topic/temperature", 0, false, str)
        token.Wait()
        if token.Error() != nil {
            fmt.Println("problems with pub")
        }
        fmt.Println("published")
        time.Sleep(1000 * time.Millisecond)
    }
    fmt.Println("finished")
    client.Disconnect(1)
}

我期望的结果是发布每个编号的 hello,但只有很少的消息通过。这是我的代理配置文件,我认为它具有使其工作的最低配置:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

log_dest file /var/log/mosquitto/mosquitto.log
log_type debug

allow_anonymous true

port 1883
listener 8883

我可以说我在使用 paho go 库时做错了,因为我使用 python paho mqtt 库用 python 完成了一个类似的脚本,它运行良好:

import paho.mqtt.client as mqtt
import time

topic1 = "topic/temperature"

mqttClient = mqtt.Client()

mqttClient.connect([ip], 1883, 60)
mqttClient.loop_start()
for i in range(10000):
    mqttClient.publish(topic1, "hello - " + str(i), qos= 0)
    #time.sleep(1)
mqttClient.loop_stop()
mqttClient.disconnect()

我也尝试过 chaging QOS,但结果相似。我错过了什么?

【问题讨论】:

  • 您的应用程序日志显示了什么,是否有失败条目?
  • 是的,有一堆 Connection lost, reason: EOF Client connected 我忘了提到这两个脚本都在我的树莓派上运行。如果我在我的计算机上运行相同的脚本,它会更好地工作,不会丢失连接,但它仍然会丢失一些消息。
  • 启用日志记录后是否会产生有用的信息? github.com/eclipse/paho.mqtt.golang#logging
  • 检查 Mosquitto 日志也是有益的;一个可能的原因是另一个客户端使用相同的客户端 ID(MQTT 要求在启动新连接时删除与新连接具有相同客户端 ID 的现有连接)。
  • 问题在于,正如@Brits 建议的那样,有另一台机器使用相同的 clientid 发布。

标签: go mqtt mosquitto paho


【解决方案1】:

根据 cmets - 问题是由于另一个客户端使用相同的客户端 ID。最简单的检查方法是读取代理日志(从客户端的角度来看,连接只是在没有警告的情况下断开)。

The MQTT spec 状态:

服务器必须处理从客户端发送的第二个 CONNECT 数据包作为协议违规并断开客户端 [MQTT-3.1.0-2]。有关处理错误的信息,请参阅第 4.8 节。

这是一个相当普遍的问题(并且是项目自述文件的common problems 部分中提到的第一件事)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-06
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多