Skip to content

allow to set socket timeout #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# Modified Work Copyright (c) 2019 Bradley Beach, esp32spi_mqtt
# Modified Work Copyright (c) 2012-2019 Roger Light and others, Paho MQTT Python

# pylint: disable=too-many-lines

"""
`adafruit_minimqtt`
================================================================================
Expand Down Expand Up @@ -129,6 +131,8 @@ class MQTT:
: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.
:param int socket_timeout: How often to check socket state for read/write/connect operations,
in seconds.

"""

Expand All @@ -145,13 +149,15 @@ def __init__(
socket_pool=None,
ssl_context=None,
use_binary_mode=False,
socket_timeout=1,
):

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._socket_timeout = socket_timeout

self.keep_alive = keep_alive
self._user_data = None
Expand Down Expand Up @@ -214,7 +220,7 @@ def _get_connect_socket(self, host, port, *, timeout=1):

:param str host: Desired broker hostname
:param int port: Desired broker port
:param int timeout: Desired socket timeout
:param int timeout: Desired socket timeout, in seconds
"""
# For reconnections - check if we're using a socket already and close it
if self._sock:
Expand Down Expand Up @@ -444,7 +450,9 @@ def connect(self, clean_session=True, host=None, port=None, keep_alive=None):
self.logger.debug("Attempting to establish MQTT connection...")

# Get a new socket
self._sock = self._get_connect_socket(self.broker, self.port)
self._sock = self._get_connect_socket(
self.broker, self.port, timeout=self._socket_timeout
)

# Fixed Header
fixed_header = bytearray([0x10])
Expand Down