3

I have multiple clients, wich i store in a list (connected). When a client (browser) is closed, i want to remove this websocket from the list of connected websockets.

I've tried the method that pgjones wrote with some little alteration (https://medium.com/@pgjones/websockets-in-quart-f2067788d1ee):

    def collect_websocket(func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            global connected
            data = await websocket.receive()
            data_json = json.loads(data)

            if data_json['msg_type'] == 'client_connect':
                if "id" in data_json:
                    new_client = Client(data_json['id'], False, websocket._get_current_object())
                    connected.add(new_client)
            try:
                return await func(*args, **kwargs)
            except Exception as e:
                connected.remove(websocket._get_current_object())
            finally:
                for connect in connected:
                    if connect.ws == websocket._get_current_object():
                        connected.remove(connect)
                    break
        return wrapper

.....

further in the code....

.....

async def update_clients(self, input_survey):
        try:
            for client in connected:
                if client.survey_id == input_survey.identifier:
                    my_websock = client.ws
                    message = { ... some message...
                                }
                    message_json = json.dumps(message)
                    await my_websock.send(message_json)
        except:
            var = traceback.format_exc()
            constants.raven_client.captureException()
            self.logger.error('ERROR updating clients: {}'.format(str(var)))

In update_clients there should be some detection that the sending to a websocket that doesnt exist anymore goes wrong... and then remove it. But there is no exception...?!

i also tried to:

try:
    await my_websock.send(message_json)
except asyncio.CancelledError:
    print('Client disconnected')
    raise

But still no exception occurred...

Pavel Smirnov
  • 4,293
  • 3
  • 14
  • 23
socialb
  • 135
  • 8
  • Its not clear what you think is going wrong here, does it work as you'd expect? – pgjones Apr 12 '19 at 09:30
  • Hi pgjones, thanks for your reply. i want to know at the server side when a client is no longer there. This so i can remove the client from the 'connected' list. What happens now, is that the 'connected' list grows and grows over time in production. Whenever a connection is gone, it stays in the list. When a new connection is created by a client it is added as it is supposed to. The purpose of 'connected' is to have only active websockets/clients in the list. – socialb Apr 12 '19 at 18:58
  • Could this be a simple issue with the `for` loop in the `finally` block, note how the break should be inside the `if` but isn't. – pgjones Apr 15 '19 at 21:33
  • Oh i see, that is a copy paste issue to stackoverflow. I tested some more. On my local machine it works correct, so when a browser tab closes, it gets removed. On my AWS ec2 it doesn't. – socialb Apr 17 '19 at 17:16

1 Answers1

2

After updating quart and hypercorn to 0.9/0.6 the code works for browsers on a pc. (Firefox, Chrome)

But when i make a connection using an iphone / ipad and close it afterwards. The connection isn't removed!

Any suggestions why?

socialb
  • 135
  • 8