flask-mqtt:Mqtt 客户端连接后立即断开连接

flask-mqtt: Mqtt Client disconnects immediately after connect

我用我当地的 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.

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

知道问题出在哪里吗?我还调试了 flask-mqtt 代码,但找不到问题。 (我的 python 版本是 Python 3.6.9(默认,2019 年 11 月 7 日,10:44:02) (我的主机系统是初级的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中最后一个例子的末尾的注释你注意了吗?

感谢您的快速回复!这对我有很大帮助并解决了问题: 代码是

"""

A small Test application to show how to use Flask-MQTT.

"""

import eventlet
import json
from flask import Flask, render_template
from flask_mqtt import Mqtt
from flask_socketio import SocketIO
from flask_bootstrap import Bootstrap

eventlet.monkey_patch()

app = Flask(__name__)
app.config['SECRET'] = 'my secret key'
app.config['TEMPLATES_AUTO_RELOAD'] = True

app.config['MQTT_BROKER_URL'] = '127.0.0.1'
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_CLIENT_ID'] = 'flask_mqtt'
#app.config['MQTT_USERNAME'] = ''
#app.config['MQTT_PASSWORD'] = ''
app.config['MQTT_KEEPALIVE'] = 30
#app.config['MQTT_TLS_ENABLED'] = False
#app.config['MQTT_REFRESH_TIME'] = 1.0  # refresh time in seconds

#app.config['MQTT_LAST_WILL_TOPIC'] = 'home/lastwill'
#app.config['MQTT_LAST_WILL_MESSAGE'] = 'bye'
#app.config['MQTT_LAST_WILL_QOS'] = 2

# Parameters for SSL enabled
# app.config['MQTT_BROKER_PORT'] = 8883
# app.config['MQTT_TLS_ENABLED'] = True
# app.config['MQTT_TLS_INSECURE'] = True
# app.config['MQTT_TLS_CA_CERTS'] = 'ca.crt'

mqtt = Mqtt(app)
socketio = SocketIO(app)
bootstrap = Bootstrap(app)


@app.route('/')
def index():
    return render_template('index.html')


@socketio.on('publish')
def handle_publish(json_str):
    data = json.loads(json_str)
    mqtt.publish(data['topic'], data['message'], data['qos'])


@socketio.on('subscribe')
def handle_subscribe(json_str):
    data = json.loads(json_str)
    mqtt.subscribe(data['topic'], data['qos'])


@socketio.on('unsubscribe_all')
def handle_unsubscribe_all():
    mqtt.unsubscribe_all()


@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
    data = dict(
        topic=message.topic,
        payload=message.payload.decode(),
        qos=message.qos,
    )
    socketio.emit('mqtt_message', data=data)


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

@mqtt.on_connect()
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)


if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000, use_reloader=True, debug=True)

更改 use_reloader=False 即可解决问题! 在这个例子中它被设置为 True ..也许应该被修复。

顺便问一下 use_reloader 是什么意思? (我是烧瓶新手)

非常感谢!