本文整理汇总了Python中hbmqtt.client.MQTTClient.publish方法的典型用法代码示例。如果您正苦于以下问题:Python MQTTClient.publish方法的具体用法?Python MQTTClient.publish怎么用?Python MQTTClient.publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hbmqtt.client.MQTTClient
的用法示例。
在下文中一共展示了MQTTClient.publish方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_coro
# 需要导入模块: from hbmqtt.client import MQTTClient [as 别名]
# 或者: from hbmqtt.client.MQTTClient import publish [as 别名]
def test_coro():
C = MQTTClient()
yield from C.connect('mqtt://test.mosquitto.org/')
tasks = [
asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_0')),
asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=QOS_1)),
asyncio.ensure_future(C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=QOS_2)),
]
yield from asyncio.wait(tasks)
logger.info("messages published")
yield from C.disconnect()
示例2: test_coro2
# 需要导入模块: from hbmqtt.client import MQTTClient [as 别名]
# 或者: from hbmqtt.client.MQTTClient import publish [as 别名]
def test_coro2():
try:
C = MQTTClient()
yield from C.connect('mqtt://test.mosquitto.org:1883/')
yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_0', qos=0x00)
yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_1', qos=0x01)
yield from C.publish('a/b', b'TEST MESSAGE WITH QOS_2', qos=0x02)
logger.info("messages published")
yield from C.disconnect()
except ConnectException as ce:
logger.error("Connection failed: %s" % ce)
asyncio.get_event_loop().stop()
示例3: test_coro
# 需要导入模块: from hbmqtt.client import MQTTClient [as 别名]
# 或者: from hbmqtt.client.MQTTClient import publish [as 别名]
def test_coro():
try:
C = MQTTClient()
yield from C.connect('mqtt://0.0.0.0:1883')
yield from C.publish('data/classified', b'TOP SECRET', qos=0x01)
yield from C.publish('data/memes', b'REAL FUN', qos=0x01)
yield from C.publish('repositories/hbmqtt/master', b'NEW STABLE RELEASE', qos=0x01)
yield from C.publish('repositories/hbmqtt/devel', b'THIS NEEDS TO BE CHECKED', qos=0x01)
yield from C.publish('calendar/hbmqtt/releases', b'NEW RELEASE', qos=0x01)
logger.info("messages published")
yield from C.disconnect()
except ConnectException as ce:
logger.error("Connection failed: %s" % ce)
asyncio.get_event_loop().stop()
示例4: test_coro
# 需要导入模块: from hbmqtt.client import MQTTClient [as 别名]
# 或者: from hbmqtt.client.MQTTClient import publish [as 别名]
def test_coro():
try:
broker = Broker(broker_config, plugin_namespace="hbmqtt.test.plugins")
yield from broker.start()
client = MQTTClient()
yield from client.connect('mqtt://127.0.0.1/')
self.assertIsNotNone(client.session)
ret = yield from client.subscribe([
('test_topic', QOS_0),
])
self.assertEqual(ret[0], QOS_0)
client_pub = MQTTClient()
yield from client_pub.connect('mqtt://127.0.0.1/')
yield from client_pub.publish('test_topic', data, QOS_0)
yield from client_pub.disconnect()
message = yield from client.deliver_message()
self.assertIsNotNone(message)
self.assertIsNotNone(message.publish_packet)
self.assertEqual(message.data, data)
yield from client.unsubscribe(['$SYS/broker/uptime'])
yield from client.disconnect()
yield from broker.shutdown()
future.set_result(True)
except Exception as ae:
future.set_exception(ae)
示例5: _client_publish
# 需要导入模块: from hbmqtt.client import MQTTClient [as 别名]
# 或者: from hbmqtt.client.MQTTClient import publish [as 别名]
def _client_publish(self, topic, data, qos, retain=False):
pub_client = MQTTClient()
ret = yield from pub_client.connect('mqtt://127.0.0.1/')
self.assertEqual(ret, 0)
ret = yield from pub_client.publish(topic, data, qos, retain)
yield from pub_client.disconnect()
return ret
示例6: test_coro
# 需要导入模块: from hbmqtt.client import MQTTClient [as 别名]
# 或者: from hbmqtt.client.MQTTClient import publish [as 别名]
def test_coro():
try:
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
yield from broker.start()
self.assertTrue(broker.transitions.is_started())
pub_client = MQTTClient()
ret = yield from pub_client.connect('mqtt://127.0.0.1/')
self.assertEqual(ret, 0)
ret_message = yield from pub_client.publish('/topic', bytearray(b'\x99' * 256 * 1024), QOS_2)
yield from pub_client.disconnect()
self.assertEqual(broker._retained_messages, {})
yield from asyncio.sleep(0.1)
yield from broker.shutdown()
self.assertTrue(broker.transitions.is_stopped())
MockPluginManager.assert_has_calls(
[
call().fire_event(EVENT_BROKER_MESSAGE_RECEIVED,
client_id=pub_client.session.client_id,
message=ret_message),
], any_order=True)
future.set_result(True)
except Exception as ae:
future.set_exception(ae)
示例7: test_coro
# 需要导入模块: from hbmqtt.client import MQTTClient [as 别名]
# 或者: from hbmqtt.client.MQTTClient import publish [as 别名]
def test_coro():
try:
broker = Broker(test_config, plugin_namespace="hbmqtt.test.plugins")
yield from broker.start()
self.assertTrue(broker.transitions.is_started())
pub_client = MQTTClient()
ret = yield from pub_client.connect('mqtt://localhost/')
self.assertEqual(ret, 0)
ret_message = yield from pub_client.publish('/topic', b'data', QOS_0, retain=True)
yield from pub_client.disconnect()
yield from asyncio.sleep(0.1)
self.assertIn('/topic', broker._retained_messages)
retained_message = broker._retained_messages['/topic']
self.assertEquals(retained_message.source_session, pub_client.session)
self.assertEquals(retained_message.topic, '/topic')
self.assertEquals(retained_message.data, b'data')
self.assertEquals(retained_message.qos, QOS_0)
yield from broker.shutdown()
self.assertTrue(broker.transitions.is_stopped())
future.set_result(True)
except Exception as ae:
future.set_exception(ae)