miguelgrinberg / simple-websocket

Simple WebSocket server and client for Python.
MIT License
78 stars 17 forks source link

when receive() raise ConnectionClosed, it ignores contents in input_buffer #30

Closed xiaosong-ai closed 12 months ago

xiaosong-ai commented 12 months ago

https://github.com/miguelgrinberg/simple-websocket/blob/6f92764754550fc85b25e42182050c1e6636a41d/src/simple_websocket/ws.py#L114

Can we release input_buffer contents to receiver before raise ConnectionClosed? The code should looks like this:

def receive(self, timeout=None):
    """Receive data over the WebSocket connection.

    :param timeout: Amount of time to wait for the data, in seconds. Set
                    to ``None`` (the default) to wait indefinitely. Set
                    to 0 to read without blocking.

    The data received is returned, as ``bytes`` or ``str``, depending on
    the type of the incoming message.
    """
    while self.connected and not self.input_buffer:
        if not self.event.wait(timeout=timeout):
            return None
        self.event.clear()
    if not self.connected:  # pragma: no cover
xiaosong-ai commented 12 months ago

the added code should be:

if not self.input_buffer:
    return self.input_buffer.pop(0)
miguelgrinberg commented 12 months ago

Yes, I think this is a reasonable change. I'll look into it.

sanchay-hai commented 12 months ago

Thanks @miguelgrinberg. We can test both #29 and #30 at the same time.

sanchay-hai commented 11 months ago

Hey @miguelgrinberg we've been using 6c87abe22215c45b3dc0dadc168c3dd061eb2aa4 successfully for the past few days