第一个脚本有效,这意味着调用回调并结束打印 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: ", rc
def on_disconnect(client, userdata, flags, rc=0):
print "Disconnect code: ", rc
def 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 time
client = mqtt.Client("Luke, I am your client")
mqtt.Client.connected_flag = False
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_log = on_log_puback
client.on_message = on_message
status = client.connect(host="test.mosquitto.org",
keepalive=60, port=1883)
print "connect status: ", status
time.sleep(2)
print "loop_start"
client.loop_start()
time.sleep(1)
sret = client.subscribe("test_topic")
print "subscribe returns sret: ", sret
time.sleep(2)
# initialize global
puback = False
# test publish with qos=1
status, msg_id = client.publish(topic="test_topic",
payload="hello!",
qos=1, retain=True)
print "publish status: ", status
time.sleep(2)
print "puback: ", puback
stop()
不工作:
import paho.mqtt.client as mqtt
import time
class 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
莫回无
相关分类