diff --git a/ws4py/websocket.py b/ws4py/websocket.py index 511921e..61f8c33 100644 --- a/ws4py/websocket.py +++ b/ws4py/websocket.py @@ -387,10 +387,12 @@ def once(self): logger.debug("WebSocket is already terminated") return False try: - b = self.sock.recv(self.reading_buffer_size) + b = b'' if self._is_secure: - b += self._get_from_pending() - if not b: + b = self._get_from_pending() + if not b and not self.buf: + b = self.sock.recv(self.reading_buffer_size) + if not b and not self.buf: return False self.buf += b except (socket.error, OSError, pyOpenSSLError) as e: @@ -403,9 +405,11 @@ def once(self): # process as much as we can # the process will stop either if there is no buffer left # or if the stream is closed - if not self.process(self.buf): + # only pass the requested number of bytes, leave the rest in the buffer + requested = self.reading_buffer_size + if not self.process(self.buf[:requested]): return False - self.buf = b"" + self.buf = self.buf[requested:] return True