stlehmann / Flask-MQTT

Flask Extension for the MQTT protocol
MIT License
207 stars 71 forks source link

Client ID is reset to b'' after the instantiation of Mqtt object #87

Open vsupacha opened 3 years ago

vsupacha commented 3 years ago

Environment Python 3.7.9 on Windows 10, Jupyter Notebook, Ubuntu 20.04 (WSL) Tested with my target broker, vanilla mosquitto docker, test.mosquitto.org Flask 1.1.2 Flask-MQTT 1.1.1 Paho-MQTT 1.5.1

Issue

  1. Internal client ID is reset to b'' even app.config[MQTT_CLIENT_ID] is set.
  2. Try a simple workaround, and it is good even string client ID (UUID style).
    app.config['MQTT_CLIENT_ID'] = 'UUID'
    mqtt = Mqtt(app)
    print(mqtt.client._client_id)
    mqtt.client._client_id = app.config['MQTT_CLIENT_ID']

Info Logger from docker

DEBUG:flask_mqtt:Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k60) client_id=b''
DEBUG:flask_mqtt:Connected client '28ceb5d2-2415-4fe9-b249-d5eeff6d78ce' to broker localhost:1883
b''
b'28ceb5d2-2415-4fe9-b249-d5eeff6d78ce'
16 Received CONNACK (0, 0)
DEBUG:flask_mqtt:Received CONNACK (0, 0)
Connected
 * Serving Flask app "testFlaskMQTT" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
INFO:werkzeug: * Running on http://127.0.0.1:5001/ (Press CTRL+C to quit) 

Log message from the target broker

DEBUG:flask_mqtt:Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k60) client_id=b''
DEBUG:flask_mqtt:Connected client '28ceb5d2-2415-4fe9-b249-d5eeff6d78ce' to broker broker.netpie.io:1883
b''
 * Serving Flask app "testFlaskMQTT" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
INFO:werkzeug: * Running on http://127.0.0.1:5001/ (Press CTRL+C to quit)
b'28ceb5d2-2415-4fe9-b249-d5eeff6d78ce'
8 failed to receive on socket: [WinError 10054] An existing connection was forcibly closed by the remote host
ERROR:flask_mqtt:failed to receive on socket: [WinError 10054] An existing connection was forcibly closed by the remote host
b'28ceb5d2-2415-4fe9-b249-d5eeff6d78ce'
16 Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k60) client_id=b'28ceb5d2-2415-4fe9-b249-d5eeff6d78ce'
DEBUG:flask_mqtt:Sending CONNECT (u1, p1, wr0, wq0, wf0, c1, k60) client_id=b'28ceb5d2-2415-4fe9-b249-d5eeff6d78ce'
b'28ceb5d2-2415-4fe9-b249-d5eeff6d78ce'
16 Received CONNACK (0, 0)
DEBUG:flask_mqtt:Received CONNACK (0, 0)
Connected
KT-network commented 1 year ago

image 另一种解决办法,将检查MQTT_CLIENT_ID的代码放到检查client_id的对象类型之前。