This commit is contained in:
yumoqing 2025-07-21 18:27:00 +08:00
parent 96581b0ac7
commit 7990cacfd9

View File

@ -24,25 +24,31 @@ class XtermProcessor(PythonScriptProcessor):
async def ws_2_process(self, ws): async def ws_2_process(self, ws):
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}.................')
data = msg.data data = DictObject(**json.loads(msg.data))
if data.type == 'input': if data.type == 'input':
self.p_obj.stdin.write(data.data) self.p_obj.stdin.write(data.data)
elif data.type == 'heartbeat': elif data.type == 'heartbeat':
await self.ws_send_heartbeat(ws) await self.ws_send_heartbeat(ws)
elif data.type == 'resize': elif data.type == 'resize':
exc = ResizeException(data.rows, data.cols) exc = ResizeException(data.rows, data.cols)
await self.p_obj.feed_exception(exc) debug(f'{exc=}, {data=}')
# self.p_obj.feed_exception(exc)
elif msg.type == aiohttp.WSMsgType.ERROR: elif msg.type == aiohttp.WSMsgType.ERROR:
# print('ws connection closed with exception %s' % ws.exception()) debug(f'ws connection closed with exception {ws.exception()}')
return return
else:
debug('recv from ws:{msg}+++++++++++')
await asyncio.sleep(0) await asyncio.sleep(0)
async def process_2_ws(self, ws): async def process_2_ws(self, ws):
while self.running: try:
x = await self.p_obj.stdout.read(1024) while self.running:
await self.ws_send_data(ws, x) x = await self.p_obj.stdout.read(1024)
await asyncio.sleep(0) await self.ws_send_data(ws, x)
await asyncio.sleep(0)
finally:
self.p_obj.close()
async def datahandle(self,request): async def datahandle(self,request):
await self.path_call(request) await self.path_call(request)
@ -71,16 +77,9 @@ class XtermProcessor(PythonScriptProcessor):
async with self.sshnode.get_connector() as conn: async with self.sshnode.get_connector() as conn:
self.running = True self.running = True
self.p_obj = await conn.create_process(term_type='xterm', term_size=(24, 80)) self.p_obj = await conn.create_process(term_type='xterm', term_size=(24, 80))
stdin_task = asyncio.create_task(self.ws_2_process(ws)) r1 = self.ws_2_process(ws)
try: r2 = self.process_2_ws(ws)
while self.running: await asyncio.gather(r1,r2)
x = await self.p_obj.stdout.read(1024)
await self.ws_send_data(ws, x)
except (asyncio.CancelledError, EOFError):
pass
finally:
self.p_obj.close()
stdin_task.cancel()
async def ws_send_heartbeat(self, ws): async def ws_send_heartbeat(self, ws):
dic = { dic = {
@ -102,6 +101,5 @@ class XtermProcessor(PythonScriptProcessor):
} }
debug(f'{data=}') debug(f'{data=}')
await ws.send_str(json.dumps(data, indent=4, ensure_ascii=False)) await ws.send_str(json.dumps(data, indent=4, ensure_ascii=False))
debug(f'{data=} sended')