From 271f8fc3ee466a0d0bd5a71543b2e50a632891dd Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Wed, 24 Aug 2022 19:36:24 +0100 Subject: [PATCH] Handle handshake with slow clients such as microcontrollers --- src/simple_websocket/ws.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/simple_websocket/ws.py b/src/simple_websocket/ws.py index 39c7b54..d11d2cc 100644 --- a/src/simple_websocket/ws.py +++ b/src/simple_websocket/ws.py @@ -411,9 +411,15 @@ def handshake(self): subprotocols=self.subprotocols or [])) self.sock.send(out_data) - in_data = self.sock.recv(self.receive_bytes) - self.ws.receive_data(in_data) - event = next(self.ws.events()) + while True: + in_data = self.sock.recv(self.receive_bytes) + self.ws.receive_data(in_data) + try: + event = next(self.ws.events()) + except StopIteration: # pragma: no cover + pass + else: # pragma: no cover + break if isinstance(event, RejectConnection): # pragma: no cover raise ConnectionError(event.status_code) elif not isinstance(event, AcceptConnection): # pragma: no cover