简体   繁体   English

aiohttp Websocket客户端和HTTP服务器

[英]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. 一些事件将来自Websocket客户端连接,一些事件将来自传入的HTTP请求。 Is it possible with aiohttp to have both of these listeners in the same application, or do I need two separate executables? 使用aiohttp是否可以将两个侦听器都放在同一个应用程序中,还是我需要两个单独的可执行文件?

I currently have a function that takes websocket events, and calls a callback on each event 我目前有一个接受websocket事件并在每个事件上调用回调的函数

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. 问题是这将永远阻塞,因此我不知道如何将其与aiohttp服务器集成。 If I do: 如果我做:

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

then web.run_app is never called. 则永远不会调用web.run_app 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. 通过使用aiohttp应用程序的on_startup处理程序,我能够实现自己的目标。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过aiohttp请求处理程序中的客户端websocket发送数据 - How to send data through a client websocket in a aiohttp request handler 如何设置 aiohttp https 服务器和客户端? - how to setup a aiohttp https server and client? 将 websocket 客户端消息路由到 websocket 服务器 - Route websocket client messages to websocket server 客户端和aiohttp Web服务器之间的短路连接 - Short circuit connection between client and aiohttp web server 如何使用aiohttp向外部服务器发出客户端请求以避免缓存 - How to make client request to external server avoiding cache using aiohttp 使用Python Twisted Websocket客户端建立与服务器的并发Websocket连接 - Establish Concurrent Websocket Connections to server Using Python Twisted Websocket Client 通过异步websocket客户端保持与websocket服务器的连接 - Keep connection with websocket server via async websocket client 在 PyGame 中实现 WebSocket Server 以通过 HTML WebSocket Client 控制对象 - Implement a WebSocket Server in PyGame to control objects via a HTML WebSocket Client Python websocket服务器命令stream(lua客户端)? - Python websocket server command stream (lua client)? Spring 引导服务器和 python 客户端使用 Websocket - Spring boot server and python client using Websocket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM