本文整理匯總了Python中Adafruit_IO.MQTTClient.subscribe方法的典型用法代碼示例。如果您正苦於以下問題:Python MQTTClient.subscribe方法的具體用法?Python MQTTClient.subscribe怎麽用?Python MQTTClient.subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Adafruit_IO.MQTTClient
的用法示例。
在下文中一共展示了MQTTClient.subscribe方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_subscribe_and_publish
# 需要導入模塊: from Adafruit_IO import MQTTClient [as 別名]
# 或者: from Adafruit_IO.MQTTClient import subscribe [as 別名]
def test_subscribe_and_publish(self):
# Create MQTT test client.
client = MQTTClient(self.get_test_username(), self.get_test_key())
# Save all on_message handler responses.
messages = []
def on_message(mqtt_client, feed, payload):
self.assertEqual(mqtt_client, client)
messages.append((feed, payload))
client.on_message = on_message
# Connect and wait until on_connect event is fired.
client.connect()
self.wait_until_connected(client)
# Subscribe to changes on a feed.
client.subscribe('testfeed')
# Publish a message on the feed.
client.publish('testfeed', 42)
# Wait for message to be received or timeout.
start = time.time()
while len(messages) == 0 and (time.time() - start) < TIMEOUT_SEC:
client.loop()
time.sleep(0)
# Verify one update message with payload is received.
self.assertListEqual(messages, [('testfeed', '42')])