From 63caea70872e875def6262c5e5d3f2da587a2711 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 14 Apr 2023 04:29:00 +0800 Subject: [PATCH] bugfix --- ahserver/websocketProcessor.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 ahserver/websocketProcessor.py diff --git a/ahserver/websocketProcessor.py b/ahserver/websocketProcessor.py new file mode 100644 index 0000000..61a3510 --- /dev/null +++ b/ahserver/websocketProcessor.py @@ -0,0 +1,31 @@ +import aiohttp +from aiohttp import web +from .baseProcessor import PythonScriptProcessor + +class WebSocketProcessor(PythonScriptProcessor): + @classmethod + def isMe(self,name): + return name=='ws' + + async def path_call(self, request,params={}): + await self.set_run_env(request) + lenv = self.run_ns.copy() + lenv.update(params) + del lenv['request'] + txt = self.loadScript(self.real_path) + exec(txt,lenv,lenv) + func = lenv['myfunc'] + ws = web.WebSocketResponse() + await ws.prepare(request) + async for msg in ws: + if msg.type == aiohttp/WSMsgType.TEXT: + if msg.data == 'close': + await ws.close() + else: + lenv['ws_data'] = msg.data + resp = await func(request,**lenv) + await ws.send_str(resp) + elif msg.type == aiohttp.WSMsgType.ERROR: + print('ws connection closed with exception %s' % ws.exception()) + return ws +