简体   繁体   中英

aiohttp Websocket client and HTTP server

I'm writing an application that needs to take in events from two sources. Some events will come from a Websocket client connection, and some will come from incoming HTTP requests. Is it possible with aiohttp to have both of these listeners in the same application, or do I need two separate executables?

I currently have a function that takes websocket events, and calls a callback on each event

async def callback(msg):
    print(msg)

async def websocket():
    session = aiohttp.ClientSession()
    async with session.ws_connect('http://example.org/websocket') as ws:

        async for msg in ws:
            if msg.type == aiohttp.WSMsgType.TEXT:
                await callback(msg.data)
            elif msg.type == aiohttp.WSMsgType.CLOSED:
                break
            elif msg.type == aiohttp.WSMsgType.ERROR:
                break

The problem is that this blocks forever, so I don't know how I could integrate this with the aiohttp server. If I do:

await websocket()
aiohttp.web.run_app(app)

then web.run_app is never called. Is there some way to accomplish this? It seems like the ideal case for asyncio, multiple things handling multiple events asynchronously. How do I do it?

I was able to accomplish my goals by using the on_startup handler of my aiohttp app.

async def callback(msg):
    print(msg)

async def websocket(session):
    async with session.ws_connect('http://example.org/websocket') as ws:
        async for msg in ws:
            if msg.type == aiohttp.WSMsgType.TEXT:
                await callback(msg.data)
            elif msg.type == aiohttp.WSMsgType.CLOSED:
                break
            elif msg.type == aiohttp.WSMsgType.ERROR:
                break

async def init(app):
    session = aiohttp.ClientSession()
    app['websocket_task'] = app.loop.create_task(websocket(session))

app = web.Application()
app.on_startup.append(init)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM