Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ HTML_CONTENT = """
|
|
| 20 |
<style>
|
| 21 |
body { margin: 0; background: #1e1e1e; overflow: hidden; }
|
| 22 |
#terminal { height: 100vh; width: 100vw; }
|
| 23 |
-
#status { position: absolute; top: 10px; right: 10px; color: #0f0; background: rgba(0,0,0,0.7); padding: 5px 10px; border-radius: 4px; font-family: sans-serif; font-size: 12px; pointer-events: none; }
|
| 24 |
</style>
|
| 25 |
</head>
|
| 26 |
<body>
|
|
@@ -38,7 +38,6 @@ HTML_CONTENT = """
|
|
| 38 |
term.open(document.getElementById('terminal'));
|
| 39 |
fitAddon.fit();
|
| 40 |
|
| 41 |
-
// 关键:直接连接当前域名的 /ws 路径
|
| 42 |
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 43 |
const wsUrl = `${protocol}//${window.location.host}/ws`;
|
| 44 |
|
|
@@ -94,8 +93,8 @@ async def handle_ws(request):
|
|
| 94 |
try:
|
| 95 |
reader, writer = await asyncio.open_connection('127.0.0.1', TTYD_PORT)
|
| 96 |
except Exception as e:
|
| 97 |
-
await ws.close()
|
| 98 |
print(f"Failed to connect to ttyd: {e}")
|
|
|
|
| 99 |
return ws
|
| 100 |
|
| 101 |
async def ws_to_tty():
|
|
@@ -115,7 +114,7 @@ async def handle_ws(request):
|
|
| 115 |
try:
|
| 116 |
while True:
|
| 117 |
data = await reader.read(4096)
|
| 118 |
-
if not
|
| 119 |
break
|
| 120 |
await ws.send_bytes(data)
|
| 121 |
except Exception:
|
|
@@ -130,7 +129,6 @@ async def on_startup(app):
|
|
| 130 |
# 启动 ttyd 子进程
|
| 131 |
cmd = ["/usr/local/bin/ttyd", "-p", str(TTYD_PORT), "/bin/bash"]
|
| 132 |
print(f"Starting ttyd: {' '.join(cmd)}")
|
| 133 |
-
# 使用 subprocess.Popen 确保它在后台运行且不阻塞
|
| 134 |
subprocess.Popen(cmd)
|
| 135 |
print("ttyd started.")
|
| 136 |
|
|
|
|
| 20 |
<style>
|
| 21 |
body { margin: 0; background: #1e1e1e; overflow: hidden; }
|
| 22 |
#terminal { height: 100vh; width: 100vw; }
|
| 23 |
+
#status { position: absolute; top: 10px; right: 10px; color: #0f0; background: rgba(0,0,0,0.7); padding: 5px 10px; border-radius: 4px; font-family: sans-serif; font-size: 12px; pointer-events: none; z-index: 999; }
|
| 24 |
</style>
|
| 25 |
</head>
|
| 26 |
<body>
|
|
|
|
| 38 |
term.open(document.getElementById('terminal'));
|
| 39 |
fitAddon.fit();
|
| 40 |
|
|
|
|
| 41 |
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 42 |
const wsUrl = `${protocol}//${window.location.host}/ws`;
|
| 43 |
|
|
|
|
| 93 |
try:
|
| 94 |
reader, writer = await asyncio.open_connection('127.0.0.1', TTYD_PORT)
|
| 95 |
except Exception as e:
|
|
|
|
| 96 |
print(f"Failed to connect to ttyd: {e}")
|
| 97 |
+
await ws.close()
|
| 98 |
return ws
|
| 99 |
|
| 100 |
async def ws_to_tty():
|
|
|
|
| 114 |
try:
|
| 115 |
while True:
|
| 116 |
data = await reader.read(4096)
|
| 117 |
+
if not data: # 【修复点】这里补全了判断条件
|
| 118 |
break
|
| 119 |
await ws.send_bytes(data)
|
| 120 |
except Exception:
|
|
|
|
| 129 |
# 启动 ttyd 子进程
|
| 130 |
cmd = ["/usr/local/bin/ttyd", "-p", str(TTYD_PORT), "/bin/bash"]
|
| 131 |
print(f"Starting ttyd: {' '.join(cmd)}")
|
|
|
|
| 132 |
subprocess.Popen(cmd)
|
| 133 |
print("ttyd started.")
|
| 134 |
|