hanli/web/process.html
hant 081903563a Fix line breaks in process output handling
Issues fixed:
- GameProcessServer.readProcessOutput() was using fgets() which could cause
  line breaks to be mishandled when reading multiple lines at once
- Changed to use fread() with 8KB buffer for non-blocking I/O
  - fread() is better for interactive programs
  - Properly handles newlines within chunks
  - Doesn't break lines unnecessarily

Frontend improvements:
- Changed sendInput() to use terminal.writeln() instead of write() + write('\n')
  - Ensures consistent line ending handling
  - Cleaner terminal output

Changes:
1. GameProcessServer.php (readProcessOutput):
   - Replaced fgets() loop with fread() loops
   - 8192-byte buffer size for optimal performance
   - Better handles non-blocking I/O streams

2. web/process.html (sendInput):
   - Use terminal.writeln() for user input display
   - More consistent with xterm.js behavior

Results:
- Line breaks now display correctly
- Output formatting preserved
- Better handling of rapid output
- No double line breaks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 13:35:36 +08:00

387 lines
11 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 {
background: #16213e;
padding: 10px 20px;
border-top: 1px solid #0f3460;
display: flex;
gap: 10px;
}
.input-area input {
flex: 1;
padding: 8px 12px;
border: 1px solid #0f3460;
border-radius: 4px;
background: #1a1a2e;
color: #eee;
font-size: 14px;
font-family: 'Consolas', 'Monaco', monospace;
}
.input-area input:focus {
outline: none;
border-color: #e94560;
}
.input-area button {
padding: 8px 16px;
background: #e94560;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
}
.input-area button:hover {
background: #ff6b6b;
}
.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 class="loading">正在连接游戏服务器...</div>
</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;
// 初始化终端
function initTerminal() {
if (terminal) return;
terminal = new Terminal({
cursorBlink: true,
fontSize: 14,
fontFamily: 'Consolas, Monaco, monospace',
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
});
fitAddon = new FitAddon.FitAddon();
terminal.loadAddon(fitAddon);
terminal.open(document.getElementById('terminal'));
fitAddon.fit();
window.addEventListener('resize', () => {
fitAddon.fit();
});
}
// 连接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('');
}
// 启用输入
document.getElementById('game-input').disabled = false;
document.getElementById('send-btn').disabled = false;
document.getElementById('game-input').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.write(data.text);
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 sendInput() {
const inputEl = document.getElementById('game-input');
const input = inputEl.value;
inputEl.value = '';
if (!input) return;
// 立即在终端显示用户输入(带换行)
if (terminal) {
terminal.writeln(input);
}
// 发送到服务器
sendMessage({
type: 'input',
input: input
});
inputEl.focus();
}
// 断开连接
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';
}
}
// 键盘处理
document.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && document.activeElement.id === 'game-input') {
sendInput();
}
});
// 心跳保活
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>