Paho 从 on_message 回调中更新用户数据
Paho updating userdata from on_message callback
基本上,我希望我的用户数据充当全局标志,在 on_message()
回调的连续执行之间读取和更新。我写了 运行 以下脚本:
import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.hivemq.com"
MQTT_TOPIC = "IvanHu"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(userdata, end = '')
print(" message received.")
userdata += 1
user_data_set(client_userdata)
client_userdata = 1
client = mqtt.Client(userdata=client_userdata)
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
client.loop_forever()
然后我在IvanHu这个话题上发表了4次。我希望我的 python paho 脚本的输出如下:
Connected with result code 0
1 message received.
2 message received.
3 message received.
4 message received.
然而,这是我得到的:
Connected with result code 0
1 message received.
1 message received.
1 message received.
1 message received.
显然,变量 client_userdata 从未在连续的 on_message() 调用中得到更新。更新此变量的正确方法是什么?
您似乎错过了文档中关于更新 user_data
对象的内容
It may be updated at a later point with the user_data_set() function.
取自文档here
这意味着您需要将新值传递给 set_userdata()
,因此它应该如下所示:
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(userdata, end = '')
print(" message received.")
userdata += 1
client.user_data_set(userdata)
我找不到使用用户数据方法来满足我的要求的方法,但我可以使用全局变量方法来满足我的要求。这是工作脚本:
import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.hivemq.com"
MQTT_TOPIC = "IvanHu"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global client_userdata
print(client_userdata, end = '')
print(" message received.")
client_userdata += 1
client_userdata = 1
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
client.loop_forever()
输出:
Connected with result code 0
1 message received.
2 message received.
3 message received.
4 message received.
你必须在连接前client.user_data_set
给用户数据一个初始值,然后你可以在on_message
回调中更新用户数据。
import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.hivemq.com"
MQTT_TOPIC = "IvanHu"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(userdata, end = '')
print(" message received.")
user_data_set(userdata + 1) # <-- Update userdata
client_userdata = 1
client = mqtt.Client(userdata=client_userdata)
client.on_connect = on_connect
client.on_message = on_message
client.user_data_set(1) # <-- Initialize
client.connect(MQTT_SERVER, 1883, 60)
client.loop_forever()
基本上,我希望我的用户数据充当全局标志,在 on_message()
回调的连续执行之间读取和更新。我写了 运行 以下脚本:
import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.hivemq.com"
MQTT_TOPIC = "IvanHu"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(userdata, end = '')
print(" message received.")
userdata += 1
user_data_set(client_userdata)
client_userdata = 1
client = mqtt.Client(userdata=client_userdata)
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
client.loop_forever()
然后我在IvanHu这个话题上发表了4次。我希望我的 python paho 脚本的输出如下:
Connected with result code 0
1 message received.
2 message received.
3 message received.
4 message received.
然而,这是我得到的:
Connected with result code 0
1 message received.
1 message received.
1 message received.
1 message received.
显然,变量 client_userdata 从未在连续的 on_message() 调用中得到更新。更新此变量的正确方法是什么?
您似乎错过了文档中关于更新 user_data
对象的内容
It may be updated at a later point with the user_data_set() function.
取自文档here
这意味着您需要将新值传递给 set_userdata()
,因此它应该如下所示:
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(userdata, end = '')
print(" message received.")
userdata += 1
client.user_data_set(userdata)
我找不到使用用户数据方法来满足我的要求的方法,但我可以使用全局变量方法来满足我的要求。这是工作脚本:
import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.hivemq.com"
MQTT_TOPIC = "IvanHu"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global client_userdata
print(client_userdata, end = '')
print(" message received.")
client_userdata += 1
client_userdata = 1
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, 1883, 60)
client.loop_forever()
输出:
Connected with result code 0
1 message received.
2 message received.
3 message received.
4 message received.
你必须在连接前client.user_data_set
给用户数据一个初始值,然后你可以在on_message
回调中更新用户数据。
import paho.mqtt.client as mqtt
MQTT_SERVER = "broker.hivemq.com"
MQTT_TOPIC = "IvanHu"
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MQTT_TOPIC)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(userdata, end = '')
print(" message received.")
user_data_set(userdata + 1) # <-- Update userdata
client_userdata = 1
client = mqtt.Client(userdata=client_userdata)
client.on_connect = on_connect
client.on_message = on_message
client.user_data_set(1) # <-- Initialize
client.connect(MQTT_SERVER, 1883, 60)
client.loop_forever()