Skip to content

Commit

Permalink
Merge pull request adafruit#97 from goliothlabs/feat/add-binary-support
Browse files Browse the repository at this point in the history
feat: add support for binary messages
  • Loading branch information
brentru authored Jan 11, 2022
2 parents 33161d4 + 53277e9 commit 113f1b2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class MQTT:
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
:param socket socket_pool: A pool of socket resources available for the given radio.
:param ssl_context: SSL context for long-lived SSL connections.
:param bool use_binary_mode: Messages are passed as bytearray instead of string to callbacks.
"""

Expand All @@ -141,12 +142,14 @@ def __init__(
keep_alive=60,
socket_pool=None,
ssl_context=None,
use_binary_mode=False,
):

self._socket_pool = socket_pool
self._ssl_context = ssl_context
self._sock = None
self._backwards_compatible_sock = False
self._use_binary_mode = use_binary_mode

self.keep_alive = keep_alive
self._user_data = None
Expand Down Expand Up @@ -839,8 +842,9 @@ def _wait_for_msg(self, timeout=0.1):
pid = pid[0] << 0x08 | pid[1]
sz -= 0x02
# read message contents
msg = self._sock_exact_recv(sz)
self._handle_on_message(self, topic, str(msg, "utf-8"))
raw_msg = self._sock_exact_recv(sz)
msg = raw_msg if self._use_binary_mode else str(raw_msg, "utf-8")
self._handle_on_message(self, topic, msg)
if res[0] & 0x06 == 0x02:
pkt = bytearray(b"\x40\x02\0\0")
struct.pack_into("!H", pkt, 2, pid)
Expand Down

0 comments on commit 113f1b2

Please sign in to comment.