Skip to content

Commit 1bdaca8

Browse files
committed
feat: add support for binary messages
1 parent 391aa36 commit 1bdaca8

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class MQTT:
126126
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
127127
:param socket socket_pool: A pool of socket resources available for the given radio.
128128
:param ssl_context: SSL context for long-lived SSL connections.
129+
:param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks.
129130
130131
"""
131132

@@ -141,12 +142,14 @@ def __init__(
141142
keep_alive=60,
142143
socket_pool=None,
143144
ssl_context=None,
145+
use_binary_mode=False,
144146
):
145147

146148
self._socket_pool = socket_pool
147149
self._ssl_context = ssl_context
148150
self._sock = None
149151
self._backwards_compatible_sock = False
152+
self._use_binary_mode = use_binary_mode
150153

151154
self.keep_alive = keep_alive
152155
self._user_data = None
@@ -839,8 +842,9 @@ def _wait_for_msg(self, timeout=0.1):
839842
pid = pid[0] << 0x08 | pid[1]
840843
sz -= 0x02
841844
# read message contents
842-
msg = self._sock_exact_recv(sz)
843-
self._handle_on_message(self, topic, str(msg, "utf-8"))
845+
raw_msg = self._sock_exact_recv(sz)
846+
msg = raw_msg if self._use_binary_mode else str(raw_msg, "utf-8")
847+
self._handle_on_message(self, topic, msg)
844848
if res[0] & 0x06 == 0x02:
845849
pkt = bytearray(b"\x40\x02\0\0")
846850
struct.pack_into("!H", pkt, 2, pid)

0 commit comments

Comments
 (0)