@@ -224,23 +224,20 @@ def connect(self, clean_session=True):
224224 self .broker , port = self .broker .split (":" , 1 )
225225 port = int (port )
226226
227+ addr = _the_sock .getaddrinfo (self .broker , self .port )[0 ]
228+ self ._sock = _the_sock .socket (addr [0 ], _the_sock .SOCK_STREAM , addr [2 ])
229+ self ._sock .settimeout (15 )
227230 if self .port == 8883 :
228231 try :
229232 if self .logger is not None :
230233 self .logger .debug ('Attempting to establish secure MQTT connection...' )
231- self ._sock .connect (( self . broker , self . port ) , TLS_MODE )
232- except RuntimeError :
233- raise MMQTTException ("Invalid broker address defined." )
234+ self ._sock .connect (addr [ - 1 ] , TLS_MODE )
235+ except RuntimeError as e :
236+ raise MMQTTException ("Invalid broker address defined." , e )
234237 else :
235- if isinstance (self .broker , str ):
236- addr = _the_sock .getaddrinfo (self .broker , self .port )[0 ]
237- else :
238- addr = (self .broker , 0x21 , self .port )
239238 try :
240239 if self .logger is not None :
241240 self .logger .debug ('Attempting to establish insecure MQTT connection...' )
242- self ._sock = _the_sock .socket (addr [0 ], 0x21 , addr [2 ])
243- self ._sock .settimeout (15 )
244241 self ._sock .connect (addr [- 1 ], TCP_MODE )
245242 except RuntimeError as e :
246243 raise MMQTTException ("Invalid broker address defined." , e )
@@ -571,49 +568,6 @@ def unsubscribe(self, topic):
571568 self ._subscribed_topics .remove (t )
572569 return
573570
574- @property
575- def is_wifi_connected (self ):
576- """Returns if the ESP module is connected to
577- an access point, resets module if False"""
578- if self ._wifi :
579- return self ._wifi .esp .is_connected
580- raise MMQTTException ("MiniMQTT Client does not use a WiFi NetworkManager." )
581-
582- # pylint: disable=line-too-long, protected-access
583- @property
584- def is_sock_connected (self ):
585- """Returns if the socket is connected."""
586- return self .is_wifi_connected and self ._sock and self ._wifi .esp .socket_connected (self ._sock ._socknum )
587-
588- def reconnect_socket (self ):
589- """Re-establishes the socket's connection with the MQTT broker.
590- """
591- try :
592- if self .logger is not None :
593- self .logger .debug ("Attempting to reconnect with MQTT Broker..." )
594- self .reconnect ()
595- except RuntimeError as err :
596- if self .logger is not None :
597- self .logger .debug ('Failed to reconnect with MQTT Broker, retrying...' , err )
598- time .sleep (1 )
599- self .reconnect_socket ()
600-
601- def reconnect_wifi (self ):
602- """Reconnects to WiFi Access Point and socket, if disconnected.
603- """
604- while not self .is_wifi_connected :
605- try :
606- if self .logger is not None :
607- self .logger .debug ('Connecting to WiFi AP...' )
608- self ._wifi .connect ()
609- except (RuntimeError , ValueError ):
610- if self .logger is not None :
611- self .logger .debug ('Failed to reset WiFi module, retrying...' )
612- time .sleep (1 )
613- # we just reconnected, is the socket still connected?
614- if not self .is_sock_connected :
615- self .reconnect_socket ()
616-
617571 def reconnect (self , resub_topics = True ):
618572 """Attempts to reconnect to the MQTT broker.
619573 :param bool resub_topics: Resubscribe to previously subscribed topics.
@@ -634,27 +588,19 @@ def loop_forever(self):
634588 """Starts a blocking message loop. Use this
635589 method if you want to run a program forever.
636590 Code below a call to this method will NOT execute.
637- Network reconnection is handled within this call.
591+
592+ NOTE: Network reconnection is not handled within this call and
593+ must be handled by your code for each interface.
638594
639595 """
640596 while True :
641- # Check WiFi and socket status
642- if self .is_sock_connected :
643- try :
644- self .loop ()
645- except (RuntimeError , ValueError ):
646- if self ._wifi :
647- # Reconnect the WiFi module and the socket
648- self .reconnect_wifi ()
649- continue
597+ if self ._sock .connected :
598+ self .loop ()
650599
651600 def loop (self ):
652601 """Non-blocking message loop. Use this method to
653602 check incoming subscription messages.
654603
655- This method does NOT handle networking or
656- network hardware management, use loop_forever
657- or handle in code instead.
658604 """
659605 if self ._timestamp == 0 :
660606 self ._timestamp = time .monotonic ()
@@ -674,7 +620,6 @@ def _wait_for_msg(self, timeout=30):
674620 """
675621 res = self ._sock .recv (1 )
676622 self ._sock .settimeout (timeout )
677- print ("RES: " , res )
678623 if res in [None , b"" ]:
679624 return None
680625 if res == MQTT_PINGRESP :
0 commit comments