为了账号安全,请及时绑定邮箱和手机立即绑定

蚊子的持久性不一致

蚊子的持久性不一致

Go
料青山看我应如是 2022-07-11 15:34:54
我在 mosquitto 上看到消息持久性和 qos=2 的消息传递不一致。有什么我做错了吗?我有一个简单的测试应用程序,它使用 clientId="receive-client" 注册消费主题,但立即断开连接。然后它以 clientId="send-client" 的身份连接并发布 10 条消息,“message #1”...“message #10”。然后断开连接,等待五秒钟,然后在打印和计算收到的消息时再次连接以使用“receive-client”消费。结果不一致。有时我收到 6 条消息,有时是 8 条。典型的输出是这样的:WARN[0005] GOT A MESSAGE:message #1                     WARN[0005] GOT A MESSAGE:message #2                     WARN[0005] GOT A MESSAGE:message #3                     WARN[0005] GOT A MESSAGE:message #4                     WARN[0005] GOT A MESSAGE:message #5                     WARN[0005] GOT A MESSAGE:message #6                     WARN[0005] GOT A MESSAGE:message #7                     WARN[0005] GOT A MESSAGE:message #8                     WARN[0305] PAUSE                                        WARN[0605] received message count=8                     我的版本信息是 1.4.15。我的 mosquitto.conf 是:pid_file /var/run/mosquitto.pidpersistence truepersistence_location /var/lib/mosquitto/allow_anonymous falsepassword_file /etc/mosquitto/passwdlog_dest file /var/log/mosquitto/mosquitto.log最初 /var/lib/mosquitto/mosquitto.db 直到运行了几次迭代才会出现。我的测试应用在这里:import (    mqtt "github.com/eclipse/paho.mqtt.golang"    log "github.com/sirupsen/logrus"    "time")var receivedMsg intfunc Persist() {    const TOPIC = "test"    const URL = "tcp://localhost:1883"    const USERNAME = "myuser"    const PASSWORD = "mypassword"    defer printReceived()    options := mqtt.NewClientOptions().AddBroker(URL).SetUsername(USERNAME).SetPassword(PASSWORD)    options.SetCleanSession(false)    options.SetConnectRetry(true)    options.SetConnectRetryInterval(10 * time.Millisecond)
查看完整描述

1 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

在 QOS 2 上发布是一个多步骤过程,因此最可能的原因是您在所有消息真正完成发布到代理之前断开了发布客户端的连接。您可能应该在循环中进行发布,并使用调用返回的令牌client.publish()来等待它完成,然后再断开客户端。


例如,如示例所示:


//Publish 5 messages to /go-mqtt/sample at qos 1 and wait for the receipt

//from the server after sending each message

for i := 0; i < 5; i++ {

  text := fmt.Sprintf("this is msg #%d!", i)

  token := c.Publish("go-mqtt/sample", 0, false, text)

  token.Wait()

}


查看完整回答
反对 回复 2022-07-11
  • 1 回答
  • 0 关注
  • 142 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号