Skip to content

Commit b66c68e

Browse files
puuupfalcon
authored andcommitted
umqtt.simple: Let subscribe() process incoming async PUBLISH messages.
Utilising retained messages and multiple subscribes, a message could arrive before the SUBACK packet. Therefore, wait_msg() must be called in subscribe().
1 parent a261f4b commit b66c68e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

umqtt.simple/umqtt/simple.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,15 @@ def subscribe(self, topic, qos=0):
103103
self.sock.write(pkt)
104104
self._send_str(topic)
105105
self.sock.write(qos.to_bytes(1))
106-
resp = self.sock.read(5)
107-
#print(resp)
108-
assert resp[0] == 0x90
109-
assert resp[2] == pkt[2] and resp[3] == pkt[3]
110-
if resp[4] == 0x80:
111-
raise MQTTException(resp[4])
106+
while 1:
107+
op = self.wait_msg()
108+
if op == 0x90:
109+
resp = self.sock.read(4)
110+
#print(resp)
111+
assert resp[1] == pkt[2] and resp[2] == pkt[3]
112+
if resp[3] == 0x80:
113+
raise MQTTException(resp[3])
114+
return
112115

113116
# Wait for a single incoming MQTT message and process it.
114117
# Subscribed messages are delivered to a callback previously

0 commit comments

Comments
 (0)