gpt4 book ai didi

python - flask -mqtt : Mqtt Client disconnects immediately after connect

转载 作者:行者123 更新时间:2023-12-01 06:30:07 26 4
gpt4 key购买 nike

我使用本地 mosquitto 代理尝试了 Flask-MQTT ( https://github.com/stlehmann/Flask-MQTT ) 的示例项目。但不幸的是它不起作用。订阅和发布未正确转发。所以我添加了一些记录器消息:

def handle_connect(client, userdata, flags, rc):
print("CLIENT CONNECTED")

@mqtt.on_disconnect()
def handle_disconnect():
print("CLIENT DISCONNECTED")

@mqtt.on_log()
def handle_logging(client, userdata, level, buf):
print(level, buf)

16 Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k30) client_id=b'flask_mqtt'
CLIENT DISCONNECTED
16 Received CONNACK (0, 0)
CLIENT CONNECTED
16 Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k30) client_id=b'flask_mqtt'
CLIENT DISCONNECTED
16 Received CONNACK (0, 0)

mosquitto Broker 显示它断开了 Flask 应用程序的连接,因为客户端已连接:

1580163250: New connection from 127.0.0.1 on port 1883.
1580163250: Client flask_mqtt already connected, closing old connection.
1580163250: New client connected from 127.0.0.1 as flask_mqtt (p2, c1, k30).
1580163250: No will message specified.
1580163250: Sending CONNACK to flask_mqtt (0, 0)
1580163251: New connection from 127.0.0.1 on port 1883.
1580163251: Client flask_mqtt already connected, closing old connection.
1580163251: New client connected from 127.0.0.1 as flask_mqtt (p2, c1, k30).
1580163251: No will message specified.
1580163251: Sending CONNACK to flask_mqtt (0, 0)
1580163251: Socket error on client flask_mqtt, disconnecting.

我还测试了一个简单的 python.paho mqtt 客户端示例,没有 Flask,它按预期工作。我还尝试了 Flask-mqtt 代码中的几个循环启动 self.client.loop_start()
--> self.client.loop_forever()
...没有改变任何东西。

那么你知道问题出在哪里吗?我还调试了flask-mqtt代码,但找不到问题。(我的python版本是Python 3.6.9(默认,2019年11月7日,10:44:02)(我的主机系统是elementary Linux)

也许 FLASK-MQTT 库已被弃用?任何提示或想法表示赞赏!

最佳答案

失败的原因在 mosquitto 日志中。

1580163250: New connection from 127.0.0.1 on port 1883.
1580163250: Client flask_mqtt already connected, closing old connection.
1580163250: New client connected from 127.0.0.1 as flask_mqtt (p2, c1, k30).
1580163250: No will message specified.
1580163250: Sending CONNACK to flask_mqtt (0, 0)

连接到代理的每个客户端都必须有一个唯一的客户端 ID。在这种情况下,Flask 客户端尝试使用相同的客户端 ID 与代理建立多个连接。当第二个连接启动时,代理发现客户端 ID 相同,并自动断开第一个连接。

您实际上并未提供任何代码来显示您如何设置客户端连接,因此我们无法就如何实际修复它提出任何建议。您是否注意到 github 页面上 README.md 中最后一个示例末尾的注释?

关于python - flask -mqtt : Mqtt Client disconnects immediately after connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59951747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com