Skip to content

Commit

Permalink
Add api for setting qos for pubmsg.
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaEMQ committed Jul 29, 2022
1 parent 0433dc5 commit b98864a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/mqttpub.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async def main():
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
await mqtt.asend_msg(pubmsg)
print(f"Publish packet sent.")

Expand Down
5 changes: 3 additions & 2 deletions examples/mqttsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pynng
import curio

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

async def main():
with pynng.Mqtt(address) as mqtt:
Expand All @@ -22,14 +22,15 @@ async def main():
print(f"Connection packet sent.")
submsg = pynng.Mqttmsg()
submsg.set_packet_type(8) # 0x08 Subscribe
submsg.set_subscribe_topic("topic", 0)
submsg.set_subscribe_topic("topic", 1) # topic With Qos 1
await mqtt.asend_msg(submsg)
print(f"Subscribe packet sent.")
while True:
rmsg = await mqtt.arecv_msg()
rmsg.__class__ = pynng.Mqttmsg # convert to mqttmsg
print("msg", rmsg, "arrived.")
print("type: ", rmsg.packet_type())
print("qos: ", rmsg.publish_qos())
print("topic: ", rmsg.publish_topic())
print("payload:", rmsg.publish_payload())

Expand Down
6 changes: 6 additions & 0 deletions pynng/nng.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,12 @@ def publish_topic(self):
data = ffi.cast('char *', lib.nng_mqtt_msg_get_publish_topic(self._nng_msg, tlenp))
return bytes(ffi.buffer(data[0:tlenp[0]])).decode()

def set_publish_qos(self, qos):
lib.nng_mqtt_msg_set_publish_qos(self._nng_msg, qos)

def publish_qos(self):
return lib.nng_mqtt_msg_get_publish_qos(self._nng_msg)

def set_subscribe_topic(self, topic, qos):
topics = lib.nng_mqtt_topic_qos_array_create(1)
lib.nng_mqtt_topic_qos_array_set(topics, 0, to_char(topic), qos)
Expand Down

0 comments on commit b98864a

Please sign in to comment.