MQTT如何工作?如果发送方文件之后启动了接收方文件,则无法获取数据 [英] How does the MQTT works? unable to get the data if the receiver file is started after the sender file

查看:200
本文介绍了MQTT如何工作?如果发送方文件之后启动了接收方文件,则无法获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MQTT的新手,我要打印一个简单的数字范围,我创建了2个文件,其中第一个文件将数据发送到第二个文件和脚本,如下所示:

I'm new in MQTT there is a simple range of numbers which I want to print I have created 2 files in which the 1st file whose send data to the 2nd file and the script is like that:

sender.py

import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("192.168.1.169", 1883, 60)
for i in range(1,100):
    client.publish("TestTopic", i)
    print(i)
client.disconnect()

receiver.py:

receiver.py:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    client.subscribe("house/bulbs/bulb1")


def on_message(client, userdata, msg):
    # print(msg.topic+" "+str(msg.payload))
    print("message received ", str(msg.payload.decode("utf-8")))
    print("message topic=", msg.topic)
    print("message qos=", msg.qos)
    print("message retain flag=", msg.retain)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("192.168.1.169", 1883, 60)
client.loop_forever()

如果接收方文件处于活动状态,我可以打印数据,但是如果我启动发送方文件然后启动接收方文件,则在打印数据时遇到问题,主要问题是MQTT是否遵循队列是还是不是机制,如果是,则....如果我正在运行发送方文件,则其所有数据都应在队列中,然后在运行其他文件(即接收方)时,应进行打印. .但是它不能以相同的方式工作,请帮助我,我查阅了许多文档,但是我能够找到任何相关信息..最近,我发现 clean_session ,如果有人对此有所了解,请告诉我. ...对我的代码有任何疑问或任何疑问,请告诉我 谢谢

I'm able to print the data if the receiver file is active but I have a problem in printing it if I started the sender file and then I started the receiver file ,main question is does MQTT follows the queueing Mechanism or not if yes then ....if I'm running the sender file then its all data should be in queue and after that when I'm run the other file which is receiver then I should get printed.. but its not working in the same way please help me I went lots of documents but i'm able to find any relevant info.. recently I found clean_session if someone have knowledge about this please tell me ....have any questions related my code or anything please let me know thanks

推荐答案

MQTT是发布/订阅协议,而不是消息排队系统.

MQTT is a pub/sub protocol, not a message queuing system.

这意味着在正常情况下,发布消息时,如果没有订阅者在运行,则该消息将不会被传递.

This means under normal circumstances if there is no subscriber running when a message is published then it will not be delivered.

可以使代理为特定订阅者排队消息,但这要求订阅者在消息发布之前已连接,并且订阅的QOS大于0.然后,只要重新连接在发布后将干净会话标志设置为false并使用相同的客户端ID,则代理将传递丢失的消息.

It is possible to get the broker to queue messages for a specific subscriber, but this requires the subscriber to have been connected before the message is published and to have subscribed with a QOS of greater than 0. Then as long as it reconnects with the clean session flag set to false and the same client id after the publish then the broker will deliver the missed messages.

保留的消息有所不同.如果在保留标志设置为true的情况下发布消息,则代理将在每个订阅者订阅匹配主题时将此单个消息传递给每个订阅者.给定主题只能保留1条消息.

Retained messages are something different. If a message is published with the retained flag set to true then the broker will deliver this single message to every subscriber when they subscribe to the matching topic. There can only ever be 1 retained message for a given topic.

这篇关于MQTT如何工作?如果发送方文件之后启动了接收方文件,则无法获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆