建立了连接,但无法在PAHO MQTT客户端检索消息有效载荷

发布于 2025-01-17 10:25:09 字数 879 浏览 1 评论 0原文

我的客户端无法从 MQTT 服务器接收消息负载。我在我的电脑上设置了 mosquitto 作为服务器和发布者,并执行以下命令:

mosquitto_pub -h 192.168.1.2 -t topic/test -m "testing" -i simulator -d

我自己订阅有效并收到有效负载。

但是,我使用 paho mqtt 的客户端仅收到成功的连接,而没有有效负载消息。所以我这里有以下代码:

def on_connect(client, userdata, flags, rc):
    print("connection ok")
    client.subscribe("topic/test")

def on_message(client, userdata, msg):
    print("Message received! " + str(msg.payload))
    
    if msg.payload == "testing":
        print("Message received")
        # Do something
        ...

def main():
    #MQTT 
    broker = "192.168.1.2"  #broker ip
    client = mqtt.Client("simulator") 
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect(broker, 1883, 60)
    client.loop_start() 

    while(1):
        ...

所以它确实设法打印“连接正常”,但不是“已收到消息!...”

My client is unable to receive message payload from the MQTT server. I had set mosquitto on my PC to act as both the server and publisher and execute the following command:

mosquitto_pub -h 192.168.1.2 -t topic/test -m "testing" -i simulator -d

Subscribing myself works and payload is received.

However, my client which is using paho mqtt only received successful connection without the payload message. So i have the following code here:

def on_connect(client, userdata, flags, rc):
    print("connection ok")
    client.subscribe("topic/test")

def on_message(client, userdata, msg):
    print("Message received! " + str(msg.payload))
    
    if msg.payload == "testing":
        print("Message received")
        # Do something
        ...

def main():
    #MQTT 
    broker = "192.168.1.2"  #broker ip
    client = mqtt.Client("simulator") 
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect(broker, 1883, 60)
    client.loop_start() 

    while(1):
        ...

So it did managed to print "connection ok", but not "Messaged received! ..."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖迟 2025-01-24 10:25:09

您在 mosquitto_pub 和 Python 中使用相同的 client-idsimulator)。根据规范

如果 ClientId 表示客户端已连接到服务器,则服务器必须断开现有客户端的连接

所以发生的情况是:

  • Python 客户端连接并订阅。
  • mosquitto_pub 连接,代理断开 python 客户端连接。
  • mosquitto_pub 发布消息(但由于没有订阅任何内容,QOS 0 消息将被代理丢弃)。
  • Python客户端重新连接并再次订阅。

尝试在 mosquitto_pub 中使用不同的客户端 ID(当我以这种方式测试时,您的代码工作正常 - 在 while 中添加了 time.sleep(1) (1))。

You are using the same client-id (simulator) with mosquitto_pub and in your python. As per the spec:

If the ClientId represents a Client already connected to the Server then the Server MUST disconnect the existing Client

So what is happening is:

  • Python client connects and subscribes.
  • mosquitto_pub connects, broker disconnects python client.
  • mosquitto_pub publishes the message (but as nothing is subscribed the QOS 0 message will be dropped by the broker).
  • Python client reconnects and subscribes again.

Try using a different client-id with mosquitto_pub (your code works fine when I test it in that way - added a time.sleep(1) in the while(1)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文