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>
59 lines
1.5 KiB
PHP
Executable File
59 lines
1.5 KiB
PHP
Executable File
#!/usr/bin/env php
|
||
<?php
|
||
/**
|
||
* 进程转发 WebSocket 服务器启动脚本
|
||
* 使用方法: php websocket-process-server.php
|
||
*
|
||
* 这个服务器为每个连接启动一个独立的 bin/game 进程
|
||
* 将进程的输出实时转发给WebSocket客户端
|
||
* 将客户端输入直接写入进程的STDIN
|
||
*/
|
||
|
||
// 自动加载
|
||
require_once __DIR__ . '/vendor/autoload.php';
|
||
|
||
use Ratchet\Server\IoServer;
|
||
use Ratchet\Http\HttpServer;
|
||
use Ratchet\WebSocket\WsServer;
|
||
use Game\Core\GameProcessServer;
|
||
|
||
// 创建WebSocket服务器
|
||
$ws = new WsServer(new GameProcessServer());
|
||
|
||
// 用HTTP服务器包装
|
||
$http = new HttpServer($ws);
|
||
|
||
// 创建IO服务器,监听9002端口
|
||
$server = IoServer::factory(
|
||
$http,
|
||
9002,
|
||
'0.0.0.0'
|
||
);
|
||
|
||
echo <<<'ASCII'
|
||
╔══════════════════════════════════════════╗
|
||
║ 凡人修仙传 - 进程转发 WebSocket 服务器 ║
|
||
╚══════════════════════════════════════════╝
|
||
|
||
⚡ WebSocket 服务器启动
|
||
📍 地址: 0.0.0.0:9002
|
||
🔗 客户端连接: ws://localhost:9002
|
||
|
||
📋 运作原理:
|
||
1. 为每个WebSocket连接启动一个 php bin/game 进程
|
||
2. 实时读取进程的STDOUT/STDERR
|
||
3. 通过WebSocket发送给客户端
|
||
4. 客户端输入直接写入进程的STDIN
|
||
|
||
💡 特点:
|
||
✓ 无需修改游戏代码
|
||
✓ 每个用户独立的游戏进程
|
||
✓ 完整的ANSI颜色支持
|
||
✓ 实时交互
|
||
|
||
按 Ctrl+C 停止服务器...
|
||
|
||
ASCII;
|
||
|
||
$server->run();
|