Go to file
hant 031428add6 Implement process-forwarding WebSocket architecture
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>
2025-12-07 13:21:23 +08:00
.idea Fix web state management: handleInput now returns structured data 2025-12-07 13:14:48 +08:00
bin 法术 2025-12-02 23:13:38 +08:00
src Implement process-forwarding WebSocket architecture 2025-12-07 13:21:23 +08:00
test Implement Server-Sent Events (SSE) for real-time battle streaming 2025-12-07 10:50:30 +08:00
web Implement process-forwarding WebSocket architecture 2025-12-07 13:21:23 +08:00
.DS_Store first 2025-12-05 09:38:15 +08:00
.gitignore 技能优化 2025-12-05 17:38:10 +08:00
box.json first 2025-12-01 18:13:15 +08:00
build-phar.php first 2025-12-01 18:13:15 +08:00
build.sh first 2025-12-01 18:13:15 +08:00
composer.json Implement WebSocket real-time communication for game 2025-12-07 12:21:30 +08:00
composer.lock Implement WebSocket real-time communication for game 2025-12-07 12:21:30 +08:00
save.json Implement WebSocket real-time communication for game 2025-12-07 12:21:30 +08:00
start-server.sh first 2025-12-01 18:13:15 +08:00
websocket-process-server.php Implement process-forwarding WebSocket architecture 2025-12-07 13:21:23 +08:00
websocket-server.php Implement WebSocket real-time communication for game 2025-12-07 12:21:30 +08:00