我编写的代码的输出是on_publish('Connected', '0')('message received ', 'test1234')('message topic=', u'paho/test')代码如下:import paho.mqtt.client as mqtt import time import jsondef on_connect(client,data,flag,rc): print("Connected",str(rc))def on_publish(client,data,msg): print("on_publish")def on_message(client, userdata, message): print("message received " ,str(message.payload.decode("utf-8"))) print("message topic=",message.topic)broker_address="iot.eclipse.org"client = mqtt.Client()client.on_connect=on_connectclient.on_publish=on_publish client.on_message=on_message client.connect(broker_address,1883) #connect to broker client.loop_start() data="test1234" client.subscribe("paho/test") client.publish("paho/test",data) time.sleep(4) # wait client.loop_stop() #stop the loop
1 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
移动到电话subscribe
和publish
到里面on_connect
的回调,那么你可以肯定的是,连接已经试图订阅一个主题并发布消息之前成功。
如果经纪人出现问题,其他任何事情都会失败。
添加回答
举报
0/150
提交
取消