第一个脚本有效,这意味着调用回调并结束打印 puback: True我使用类A来完成工作的第二个脚本不起作用。回调不会被调用,它以a.puback: False我不确定我的问题是否是回调不能以这种方式工作,在这种情况下,我怎样才能让我的班级使用这些 Paho MQTT 回调?或者如果它是更微妙的东西。作品:def on_log_puback(client, userdata, level, buf): global puback if 'PUBACK' in buf: puback = True print "PUBACK!"def on_connect(client, userdata, flags, rc): print "Connect code: ", rcdef on_disconnect(client, userdata, flags, rc=0): print "Disconnect code: ", rcdef on_message(client, userdata, msg): print " Message: ", str(msg.payload.decode("utf-8", "ignore"))def stop(): print ("stopping loop") client.loop_stop() print "disconnecting" client.disconnect()import paho.mqtt.client as mqtt import timeclient = mqtt.Client("Luke, I am your client") mqtt.Client.connected_flag = False client.on_connect = on_connect client.on_disconnect = on_disconnectclient.on_log = on_log_puback client.on_message = on_messagestatus = client.connect(host="test.mosquitto.org", keepalive=60, port=1883)print "connect status: ", statustime.sleep(2)print "loop_start"client.loop_start()time.sleep(1)sret = client.subscribe("test_topic")print "subscribe returns sret: ", srettime.sleep(2)# initialize globalpuback = False# test publish with qos=1status, msg_id = client.publish(topic="test_topic", payload="hello!", qos=1, retain=True) print "publish status: ", statustime.sleep(2)print "puback: ", pubackstop()不工作:import paho.mqtt.client as mqtt import timeclass A(object): def __init__(self): client = mqtt.Client("Luke, I am your client") self.client = client mqtt.Client.connected_flag = False client.on_connect = self.on_connect client.on_disconnect = self.on_disconnect client.on_log = self.on_log_puback client.on_message = self.on_message
添加回答
举报
0/150
提交
取消