This commit is contained in:
yumoqing 2025-07-21 16:34:27 +08:00
parent 98d781a356
commit 96581b0ac7

View File

@ -11,6 +11,11 @@ from appPublic.dictObject import DictObject
from appPublic.log import info, debug, warning, error, exception, critical from appPublic.log import info, debug, warning, error, exception, critical
from .baseProcessor import BaseProcessor, PythonScriptProcessor from .baseProcessor import BaseProcessor, PythonScriptProcessor
class ResizeException(Exception):
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
class XtermProcessor(PythonScriptProcessor): class XtermProcessor(PythonScriptProcessor):
@classmethod @classmethod
def isMe(self,name): def isMe(self,name):
@ -20,24 +25,23 @@ class XtermProcessor(PythonScriptProcessor):
async for msg in ws: async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT: if msg.type == aiohttp.WSMsgType.TEXT:
debug(f'recv from ws:{msg}') debug(f'recv from ws:{msg}')
resize_pattern = '_#_resize_#_' data = msg.data
heartbeat_pattern = '_#_heartbeat_#_' if data.type == 'input':
if msg.data.startswith(resize_pattern): self.p_obj.stdin.write(data.data)
row, col = [ int(i) for i in msg.data[len(resize_pattern):].split(',')] elif data.type == 'heartbeat':
await self.p_obj.set_terminal_size(row, col) await self.ws_send_heartbeat(ws)
continue elif data.type == 'resize':
if msg.data == heartbeat_pattern: exc = ResizeException(data.rows, data.cols)
await self.ws_sendstr(ws, heartbeat_pattern) await self.p_obj.feed_exception(exc)
continue
self.p_obj.stdin.write(msg.data)
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())
return return
await asyncio.sleep(0)
async def process_2_ws(self, ws): async def process_2_ws(self, ws):
while self.running: while self.running:
x = await self.p_obj.stdout.read(1024) x = await self.p_obj.stdout.read(1024)
await self.ws_sendstr(ws, x) await self.ws_send_data(ws, x)
await asyncio.sleep(0) await asyncio.sleep(0)
async def datahandle(self,request): async def datahandle(self,request):
@ -71,14 +75,27 @@ class XtermProcessor(PythonScriptProcessor):
try: try:
while self.running: while self.running:
x = await self.p_obj.stdout.read(1024) x = await self.p_obj.stdout.read(1024)
await self.ws_sendstr(ws, x) await self.ws_send_data(ws, x)
except (asyncio.CancelledError, EOFError): except (asyncio.CancelledError, EOFError):
pass pass
finally: finally:
self.p_obj.close() self.p_obj.close()
stdin_task.cancel() stdin_task.cancel()
async def ws_sendstr(self, ws:web.WebSocketResponse, s:str): async def ws_send_heartbeat(self, ws):
dic = {
'type':'heartbeat'
}
await self.ws_send(ws, dic)
async def ws_send_data(self, ws, d):
dic = {
'type':'data',
'data':d
}
await self.ws_send(ws, dic)
async def ws_send(self, ws:web.WebSocketResponse, s):
data = { data = {
"type":1, "type":1,
"data":s "data":s