猿问

为什么在 on_connect 之前调用 on_publish 回调?

我编写的代码的输出是


on_publish

('Connected', '0')

('message received ', 'test1234')

('message topic=', u'paho/test')

代码如下:


import paho.mqtt.client as mqtt  

import time import json


def 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_connect

client.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


慕斯王
浏览 270回答 1
1回答

缥缈止盈

移动到电话subscribe和publish到里面on_connect的回调,那么你可以肯定的是,连接已经试图订阅一个主题并发布消息之前成功。如果经纪人出现问题,其他任何事情都会失败。
随时随地看视频慕课网APP

相关分类

Python
我要回答