@@ -32,6 +32,7 @@ def __init__(
3232 self .handle_request = handle_request
3333 self .response_queue = response_queue
3434 self .log = log
35+ self .client_connected = False
3536
3637 def pending (self ) -> int :
3738 """Return the number of bytes in the response buffer."""
@@ -45,31 +46,31 @@ def handle_socket_recv(self, read_size: int) -> bytes:
4546 self .response_buf .seek (0 )
4647 data = self .response_buf .read (read_size )
4748 _LOGGER .debug ("Response: 0x%s" , data .hex ())
49+ self .log .add_log_entry ("[mqtt <]" , data )
4850 # Consume the rest of the data in the buffer
4951 remaining_data = self .response_buf .read ()
5052 self .response_buf = io .BytesIO (remaining_data )
5153 return data
5254
5355 def handle_socket_send (self , client_request : bytes ) -> int :
5456 """Receive an incoming request from the client."""
57+ self .client_connected = True
5558 _LOGGER .debug ("Request: 0x%s" , client_request .hex ())
5659 self .log .add_log_entry ("[mqtt >]" , client_request )
5760 if (response := self .handle_request (client_request )) is not None :
5861 # Enqueue a response to be sent back to the client in the buffer.
5962 # The buffer will be emptied when the client calls recv() on the socket
6063 _LOGGER .debug ("Queued: 0x%s" , response .hex ())
61- self .log .add_log_entry ("[mqtt <]" , response )
6264 self .response_buf .write (response )
6365 return len (client_request )
6466
6567 def push_response (self ) -> None :
6668 """Push a response to the client."""
67- if not self .response_queue .empty ():
69+ if not self .response_queue .empty () and self . client_connected :
6870 response = self .response_queue .get ()
6971 # Enqueue a response to be sent back to the client in the buffer.
7072 # The buffer will be emptied when the client calls recv() on the socket
7173 _LOGGER .debug ("Queued: 0x%s" , response .hex ())
72- self .log .add_log_entry ("[mqtt <]" , response )
7374 self .response_buf .write (response )
7475
7576
0 commit comments