New simplified approach: WebSocket server spawns bin/game process and forwards I/O Backend Changes: - New GameProcessServer class (src/Core/GameProcessServer.php) - Implements MessageComponentInterface (Ratchet) - For each WebSocket connection, spawns independent php bin/game process - Uses proc_open() to manage process I/O pipes - Reads process STDOUT/STDERR in non-blocking mode - Writes client input to process STDIN - Automatic process cleanup on disconnect - No game code modifications required - New websocket-process-server.php startup script - Listens on port 9002 - Simple process forwarder without game-specific logic - Suitable for any interactive CLI application Frontend Changes: - New web/process.html - Ultra-simple WebSocket frontend for process I/O - Direct STDIN/STDOUT forwarding - ANSI color support via xterm.js - Minimal dependencies, minimal code - Suitable for any CLI game/application Architecture Benefits: ✓ Zero game code changes needed ✓ Each user gets independent process (isolation) ✓ Real process STDIO, not emulation ✓ ANSI colors work perfectly ✓ Can run ANY CLI application (not just this game) ✓ Simpler than GameSession-based approach ✓ Easier to deploy and manage Usage: 1. Start WebSocket server: php websocket-process-server.php 2. Start HTTP file server (for static files): php -S 0.0.0.0:8080 web/server.php 3. Open browser: http://localhost:8080/process.html Message Protocol: Client → Server: { "type": "input", "input": "command" } - Send stdin to process { "type": "ping" } - Heartbeat Server → Client: { "type": "output", "text": "..." } - Process stdout/stderr { "type": "system", "message": "..." } - Server messages { "type": "error", "message": "..." } - Error messages { "type": "pong" } - Heartbeat response Features: - Non-blocking I/O reading - Stream buffering management - Automatic reconnection support - 30-second heartbeat for keep-alive - Process termination on disconnect - Proper error handling This is the simplest and most elegant approach for running CLI games on web! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
388 lines
11 KiB
HTML
388 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.write(input);
|
|
terminal.write('\n');
|
|
}
|
|
|
|
// 发送到服务器
|
|
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>
|