Skip to content

Commit

Permalink
Add argv supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaEMQ committed Aug 1, 2022
1 parent b98864a commit 3127d46
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 9 additions & 3 deletions examples/mqttpub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
This is the only reliable messaging pattern in the suite, as it automatically will retry if a request is not matched with a response.
"""
import sys
import pynng
import curio

helper = "Usage:\n\tmqttpub.py <topic> <qos> <payload>"

address = "mqtt-quic://127.0.0.1:14567"

async def main():
Expand All @@ -20,13 +23,16 @@ async def main():
print(f"Connection packet sent.")
pubmsg = pynng.Mqttmsg()
pubmsg.set_packet_type(3) # 0x03 Publish
pubmsg.set_publish_payload("Hello")
pubmsg.set_publish_topic("topic")
pubmsg.set_publish_qos(1) # qos is 1
pubmsg.set_publish_payload(sys.argv[3])
pubmsg.set_publish_topic(sys.argv[1])
pubmsg.set_publish_qos(int(sys.argv[2]))
await mqtt.asend_msg(pubmsg)
print(f"Publish packet sent.")

if __name__ == "__main__":
if len(sys.argv) != 4:
print(helper)
exit(0)
try:
curio.run(main)
except KeyboardInterrupt:
Expand Down
8 changes: 7 additions & 1 deletion examples/mqttsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
This is the only reliable messaging pattern in the suite, as it automatically will retry if a request is not matched with a response.
"""
import sys
import pynng
import curio

helper = "Usage:\n\tmqttsub.py <topic> <qos>"

address = "mqtt-quic://127.0.0.1:14567"

async def main():
Expand All @@ -22,7 +25,7 @@ async def main():
print(f"Connection packet sent.")
submsg = pynng.Mqttmsg()
submsg.set_packet_type(8) # 0x08 Subscribe
submsg.set_subscribe_topic("topic", 1) # topic With Qos 1
submsg.set_subscribe_topic(sys.argv[1], int(sys.argv[2])) # Topic and qos
await mqtt.asend_msg(submsg)
print(f"Subscribe packet sent.")
while True:
Expand All @@ -35,6 +38,9 @@ async def main():
print("payload:", rmsg.publish_payload())

if __name__ == "__main__":
if len(sys.argv) != 3:
print(helper)
exit(0)
try:
curio.run(main)
except KeyboardInterrupt:
Expand Down

0 comments on commit 3127d46

Please sign in to comment.