This commit is contained in:
yumoqing 2024-04-25 18:08:21 +08:00
parent 6c6f43091b
commit 8e04b83ba3
2 changed files with 61 additions and 12 deletions

View File

@ -1,4 +1,5 @@
import re import re
import base64
import json import json
from traceback import print_exc from traceback import print_exc
from aiohttp import web from aiohttp import web
@ -6,6 +7,10 @@ from appPublic.dictObject import DictObject
from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM from appPublic.httpclient import HttpClient, RESPONSE_TEXT, RESPONSE_JSON, RESPONSE_BIN,RESPONSE_FILE, RESPONSE_STREAM
from appPublic.argsConvert import ArgsConvert from appPublic.argsConvert import ArgsConvert
def encode_imagefile(fn):
with open(fn, 'rb') as f:
return base64.b64encode(f.read()).decode('utf-8')
class LlmProxy: class LlmProxy:
def __init__(self, processor, desc): def __init__(self, processor, desc):
assert desc.name assert desc.name

View File

@ -78,6 +78,49 @@ class XtermProcessor(BaseProcessor):
self.sshnode.close() self.sshnode.close()
self.p_obj.close() self.p_obj.close()
class WsPool:
def __init__(self, ws_path, app):
self.app = app
self.id = None
self.ws_path = ws_path
self.data = self.get_data()
if self.data is None:
self.data = {}
self.set_data()
def get_data(self):
self.data = self.app.get_data(self.ws_path)
def set_data(self):
self.app.set_data(self.ws_path, self.data)
def set_id(id):
self.id = id
def get_ws(self, id):
self.get_data()
return self.data.get(id)
def add_ws(self, ws):
if not self.id:
return
self.data = self.get_data()
if (self.data.get(self.id)):
return
self.data.update({self.id:ws})
self.set_data()
def delete_ws(self):
if not self.id:
return
self.data = self.get_data()
if (not self.data.get(self.id)):
return
self.data = {k:v for k,v in self.data.items() if k != self.id }
self.set_data()
class WebsocketProcessor(PythonScriptProcessor): class WebsocketProcessor(PythonScriptProcessor):
@classmethod @classmethod
def isMe(self,name): def isMe(self,name):
@ -90,28 +133,29 @@ class WebsocketProcessor(PythonScriptProcessor):
} }
await ws.send_str(json.dumps(data)) await ws.send_str(json.dumps(data))
def received_data_convert(self, data):
return data
async def path_call(self, request,params={}): async def path_call(self, request,params={}):
# print('1----------------------------------')
await self.set_run_env(request) await self.set_run_env(request)
lenv = self.run_ns.copy() lenv = self.run_ns.copy()
lenv.update(params) lenv.update(params)
print(f'{request.path=}')
del lenv['request'] del lenv['request']
# print('2----------------------------------') txt = await self.loadScript(self.real_path)
txt = self.loadScript(self.real_path)
exec(txt,lenv,lenv)
func = lenv['myfunc']
# print('3----------------------------------')
ws = web.WebSocketResponse() ws = web.WebSocketResponse()
await ws.prepare(request) await ws.prepare(request)
# print('4----------------------------------', aiohttp.WSMsgType.TEXT) ws_pool = WsPool(request.path, request.app)
await self.ws_sendstr(ws, 'Welcome to websock') await self.ws_sendstr(ws, 'Welcome to websock')
async for msg in ws: async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT: if msg.type == aiohttp.WSMsgType.TEXT:
# print('msg=:', msg) print('msg=:', msg.data)
lenv['ws_data'] = msg.data lenv['ws_data'] = self.received_data_convert(msg.data)
# resp = await func(request,**lenv) lenv['ws_pool'] = ws_pool
await self.ws_sendstr(ws, msg.data) exec(txt,lenv,lenv)
# print('msg.data=', msg.data) func = lenv['myfunc']
resp = await func(request,**lenv)
await self.ws_sendstr(ws, resp)
elif msg.type == aiohttp.WSMsgType.ERROR: elif msg.type == aiohttp.WSMsgType.ERROR:
# print('ws connection closed with exception %s' % ws.exception()) # print('ws connection closed with exception %s' % ws.exception())
pass pass