amqtt/samples/client_publish_ws.py

43 wiersze
1.1 KiB
Python
Czysty Zwykły widok Historia

2015-08-02 14:01:16 +00:00
import asyncio
import logging
2015-08-02 14:01:16 +00:00
2021-03-27 12:59:48 +00:00
from amqtt.client import MQTTClient
from amqtt.mqtt.constants import QOS_1, QOS_2
2015-08-02 14:01:16 +00:00
"""
This sample shows how to publish messages to secure websocket broker
"""
2015-08-02 14:01:16 +00:00
logger = logging.getLogger(__name__)
config = {
"will": {
"topic": "/will/client",
"message": "Dead or alive",
"qos": QOS_1,
"retain": True,
}
2015-08-02 14:01:16 +00:00
}
client = MQTTClient(config=config)
2015-08-02 14:01:16 +00:00
2017-08-06 22:06:57 +00:00
async def test_coro() -> None:
await client.connect("ws://localhost:8080/")
2015-08-02 14:01:16 +00:00
tasks = [
asyncio.ensure_future(client.publish("a/b", b"TEST MESSAGE WITH QOS_0")),
asyncio.ensure_future(client.publish("a/b", b"TEST MESSAGE WITH QOS_1", qos=QOS_1)),
asyncio.ensure_future(client.publish("a/b", b"TEST MESSAGE WITH QOS_2", qos=QOS_2)),
2015-08-02 14:01:16 +00:00
]
2020-12-31 00:16:45 +00:00
await asyncio.wait(tasks)
2015-08-02 14:01:16 +00:00
logger.info("messages published")
await client.disconnect()
2015-08-02 14:01:16 +00:00
def __main__():
2015-08-02 14:01:16 +00:00
formatter = "[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, format=formatter)
asyncio.run(test_coro())
if __name__ == "__main__":
__main__()