master
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 base64
import json
from traceback import print_exc
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.argsConvert import ArgsConvert
def encode_imagefile(fn):
with open(fn, 'rb') as f:
return base64.b64encode(f.read()).decode('utf-8')
class LlmProxy:
def __init__(self, processor, desc):
assert desc.name

View File

@ -78,6 +78,49 @@ class XtermProcessor(BaseProcessor):
self.sshnode.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):
@classmethod
def isMe(self,name):
@ -90,28 +133,29 @@ class WebsocketProcessor(PythonScriptProcessor):
}
await ws.send_str(json.dumps(data))
def received_data_convert(self, data):
return data
async def path_call(self, request,params={}):
# print('1----------------------------------')
await self.set_run_env(request)
lenv = self.run_ns.copy()
lenv.update(params)
print(f'{request.path=}')
del lenv['request']
# print('2----------------------------------')
txt = self.loadScript(self.real_path)
exec(txt,lenv,lenv)
func = lenv['myfunc']
# print('3----------------------------------')
txt = await self.loadScript(self.real_path)
ws = web.WebSocketResponse()
await ws.prepare(request)
# print('4----------------------------------', aiohttp.WSMsgType.TEXT)
ws_pool = WsPool(request.path, request.app)
await self.ws_sendstr(ws, 'Welcome to websock')
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
# print('msg=:', msg)
lenv['ws_data'] = msg.data
# resp = await func(request,**lenv)
await self.ws_sendstr(ws, msg.data)
# print('msg.data=', msg.data)
print('msg=:', msg.data)
lenv['ws_data'] = self.received_data_convert(msg.data)
lenv['ws_pool'] = ws_pool
exec(txt,lenv,lenv)
func = lenv['myfunc']
resp = await func(request,**lenv)
await self.ws_sendstr(ws, resp)
elif msg.type == aiohttp.WSMsgType.ERROR:
# print('ws connection closed with exception %s' % ws.exception())
pass