Websockets with the python web framework “quart”?

為{幸葍}努か 提交于 2019-12-11 04:57:13

问题


I need help with the python web frame work, Quart, more specifically the websockets. I would like to be able to register a client when it connects (add it to a python list), and unregister them (remove it from the python list) when it disconnects. The closest thing I could find on the web is this code:

connected = set()

async def handler(websocket, path):
    global connected
    # Register.
    connected.add(websocket)
    try:
        # Implement logic here.
        await asyncio.wait([ws.send("Hello!") for ws in connected])
        await asyncio.sleep(10)
    finally:
        # Unregister.
        connected.remove(websocket)

source

But this does not work with quart websockets.

Help would be appreciated.


回答1:


This decorator when used to wrap a websocket handler, will add and remove websockets from the connected set. The _get_current_object method of the websocket is required to get the websocket in the current context, and the try-finally is required to ensure the websocket is removed regardless of any errors that are raised. Note the app.websocket must wrap (be before) the collect_websocket usage.

from functools import wraps

connected = set()

def collect_websocket(func):
    @wraps(func)
    async def wrapper(*args, **kwargs):
        global connected
        connected.add(websocket._get_current_object())
        try:
            return await func(*args, **kwargs)
        finally:
            connected.remove(websocket._get_current_object())
    return wrapper                                                                                                                                                                                                            


@app.websocket('/ws')                                                                                                                                                                                       
@collect_websocket
async def ws():
    ...

Edit: I am the Quart author.



来源:https://stackoverflow.com/questions/49837294/websockets-with-the-python-web-framework-quart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!