繁体   English   中英

如何将aiohttp websocket处理程序重写为sanic?

[英]How to rewrite aiohttp websocket handler to sanic?

我的aiohttp项目中有以下websocket handler

async def websocket_handler(request):
     ws = web.WebSocketResponse()
     await ws.prepare(request)
     request.app['websockets'].append(ws)

     async for msg in ws:
         if msg.type == aiohttp.WSMsgType.TEXT:
             if msg.data == 'close':
                 await ws.close()

         elif msg.type == aiohttp.WSMsgType.ERROR:
             logger.info('ws connection closed with exception %s' %
                            ws.exception())

     request.app['websockets'].remove(ws)
     return ws

但现在我想转向sanic框架。 如何重写这个方法? 我不明白如何从本教程中做到这一点

@bp.websocket('/websocket_handler')
    async def websocket_handler(_, ws):
        self.app['web_socket'].append(ws)
        while True:
            try:
                await ws.recv()
            except ConnectionClosed:
                break

        self.app['web_socket'].remove(ws)

暂无
暂无

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

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