umqtt.robust: Fix check_msg blocking after reconnect.

After `reconnect()`, MQTTClient.socket is blocking by default, and
check_msg() can block.  This commit aims to fix that behaviour by
reimplementing `check_msg()` for umqtt.robust and setting the socket to
non-blocking.

Fixes issue #192.
pull/551/head
Ian Cotter-Llewellyn 2022-07-18 16:36:13 +01:00 zatwierdzone przez Damien George
rodzic b50d3462d7
commit 4dc2d5e17f
3 zmienionych plików z 25 dodań i 1 usunięć

Wyświetl plik

@ -0,0 +1,14 @@
import umqtt.robust
import time
# Instantiate an MQTTClient with a keepalive time of 5 seconds (to help us test
# what happens to check_msg() with a broken connection)
m = umqtt.robust.MQTTClient(host="localhost", debug=True, keepalive=5)
m.connect()
# Wait for the broker to consider us dead
time.sleep(6)
# This should initiate a reconnect() and return immediately
m.check_msg()

Wyświetl plik

@ -1,5 +1,5 @@
metadata(
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.1"
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.2"
)
# Originally written by Paul Sokolovsky.

Wyświetl plik

@ -42,3 +42,13 @@ class MQTTClient(simple.MQTTClient):
except OSError as e:
self.log(False, e)
self.reconnect()
def check_msg(self, attempts=2):
while attempts:
self.sock.setblocking(False)
try:
return super().wait_msg()
except OSError as e:
self.log(False, e)
self.reconnect()
attempts -= 1