idle/web/process.html
2025-12-10 18:07:57 +08:00

363 lines
10 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>凡人修仙传 - 进程转发版</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #1a1a2e;
min-height: 100vh;
display: flex;
flex-direction: column;
font-family: 'Consolas', 'Monaco', monospace;
}
.container {
flex: 1;
display: flex;
flex-direction: column;
max-width: 100%;
}
.header {
background: #16213e;
padding: 15px 20px;
border-bottom: 1px solid #0f3460;
display: flex;
justify-content: space-between;
align-items: center;
}
.header h1 {
color: #eee;
font-size: 18px;
margin: 0;
}
.header-right {
display: flex;
gap: 15px;
align-items: center;
}
.status {
font-size: 12px;
color: #888;
}
.status-indicator {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
margin-left: 5px;
background: #e94560;
}
.status-indicator.connected {
background: #2ed573;
}
.btn-disconnect {
padding: 6px 12px;
background: #e94560;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
}
.btn-disconnect:hover {
background: #ff6b6b;
}
#terminal {
flex: 1;
overflow: hidden;
padding: 10px;
}
.input-area {
display: none;
}
.loading {
text-align: center;
padding: 20px;
color: #888;
font-size: 14px;
}
.error {
color: #e94560;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>🎮 凡人修仙传 - 进程转发版</h1>
<div class="header-right">
<span class="status">
连接状态: <span id="conn-status">连接中</span>
<span class="status-indicator" id="conn-indicator"></span>
</span>
<button class="btn-disconnect" onclick="disconnect()">断开连接</button>
</div>
</div>
<div class="container">
<div id="terminal">
</div>
<div class="input-area">
<input type="text" id="game-input" placeholder="输入命令..." disabled>
<button id="send-btn" onclick="sendInput()" disabled>发送</button>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/xterm@5.3.0/lib/xterm.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xterm-addon-fit@0.8.0/lib/xterm-addon-fit.min.js"></script>
<script>
let terminal = null;
let fitAddon = null;
let ws = null;
let connected = false;
let inputBuffer = ''; // 缓存用户输入
// 初始化终端
function initTerminal() {
if (terminal) return;
terminal = new Terminal({
cursorBlink: true,
fontSize: 14,
fontFamily: '"Source Code Pro", "Courier New", monospace',
letterSpacing: 0,
lineHeight: 1.3,
theme: {
background: '#1a1a2e',
foreground: '#eee',
cursor: '#e94560',
cursorAccent: '#1a1a2e',
selection: 'rgba(233, 69, 96, 0.3)',
black: '#1a1a2e',
red: '#e94560',
green: '#2ed573',
yellow: '#ffa502',
blue: '#70a1ff',
magenta: '#ff6b81',
cyan: '#1e90ff',
white: '#eee',
brightBlack: '#666',
brightRed: '#ff6b6b',
brightGreen: '#7bed9f',
brightYellow: '#ffda79',
brightBlue: '#a4b0be',
brightMagenta: '#ff7f9f',
brightCyan: '#34ace0',
brightWhite: '#fff'
},
rows: 30,
cols: 100,
allowProposedApi: true
});
fitAddon = new FitAddon.FitAddon();
terminal.loadAddon(fitAddon);
terminal.open(document.getElementById('terminal'));
fitAddon.fit();
// 监听终端输入事件
terminal.onData((data) => {
// 处理输入
if (data === '\r' || data === '\n') {
// 用户按了回车
if (inputBuffer) {
sendInput();
}
} else if (data === '\u007F') {
// 处理退格键
if (inputBuffer.length > 0) {
inputBuffer = inputBuffer.slice(0, -1);
terminal.write('\b \b');
}
} else if (data === '\u0003') {
// Ctrl+C - 中断
inputBuffer = '';
terminal.write('^C\r\n');
} else {
// 普通字符
inputBuffer += data;
terminal.write(data);
}
});
window.addEventListener('resize', () => {
fitAddon.fit();
});
}
// 发送输入到服务器
function sendInput() {
if (!inputBuffer) return;
const input = inputBuffer;
inputBuffer = '';
sendMessage({
type: 'input',
input: input
});
}
// 连接WebSocket
function connectWebSocket() {
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = protocol + '//' + location.hostname + ':9002';
ws = new WebSocket(wsUrl);
ws.onopen = () => {
console.log('[WebSocket] 已连接');
connected = true;
updateStatus(true);
initTerminal();
if (terminal) {
terminal.clear();
terminal.writeln('\x1b[32m[✓] WebSocket 连接成功,游戏进程已启动\x1b[0m');
terminal.writeln('');
terminal.focus(); // 设置终端焦点以接收输入
}
// 启动心跳
startHeartbeat();
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
handleMessage(data);
} catch (e) {
console.error('消息解析错误:', e);
}
};
ws.onerror = (error) => {
console.error('[WebSocket] 错误:', error);
if (terminal) {
terminal.writeln('\x1b[31m[✗] 连接错误\x1b[0m');
}
};
ws.onclose = () => {
console.log('[WebSocket] 已断开');
connected = false;
updateStatus(false);
if (terminal) {
terminal.writeln('\x1b[31m[✗] 连接已断开\x1b[0m');
}
document.getElementById('game-input').disabled = true;
document.getElementById('send-btn').disabled = true;
};
}
// 处理服务器消息
function handleMessage(data) {
if (!terminal) {
initTerminal();
}
switch (data.type) {
case 'output':
// terminal.clear();
const lines = data.text.split('\n');
lines.forEach(line => {
terminal.writeln(line);
});
break;
case 'system':
terminal.writeln('\x1b[33m' + data.message + '\x1b[0m');
break;
case 'error':
terminal.writeln('\x1b[31m[错误] ' + data.message + '\x1b[0m');
break;
case 'pong':
// 心跳响应
break;
default:
console.warn('未知消息类型:', data.type);
}
}
// 发送消息给服务器
function sendMessage(data) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
if (terminal) {
terminal.writeln('\x1b[31m[错误] 未连接到服务器\x1b[0m');
}
return false;
}
ws.send(JSON.stringify(data));
return true;
}
// 断开连接
function disconnect() {
if (ws) {
ws.close();
}
location.reload();
}
// 更新连接状态显示
function updateStatus(isConnected) {
const statusEl = document.getElementById('conn-status');
const indicatorEl = document.getElementById('conn-indicator');
if (isConnected) {
statusEl.textContent = '已连接';
indicatorEl.className = 'status-indicator connected';
} else {
statusEl.textContent = '已断开';
indicatorEl.className = 'status-indicator';
}
}
// 心跳保活
function startHeartbeat() {
setInterval(() => {
if (connected && ws && ws.readyState === WebSocket.OPEN) {
sendMessage({ type: 'ping' });
}
}, 30000);
}
// 页面加载完成
window.onload = () => {
connectWebSocket();
};
// 页面关闭时断开连接
window.onbeforeunload = () => {
if (ws) {
ws.close();
}
};
</script>
</body>
</html>