hanli/test-websocket.php
hant 52e465177c Fix event loop initialization and handle pipe errors
- Modified startup script to instantiate GameProcessServer separately
  before wrapping in WsServer/HttpServer, allowing direct access to
  set event loop after IoServer creation
- Implemented setLoop() method to set event loop after construction
- Added error handling for fwrite() to gracefully handle broken pipes
  when process closes
- Suppressed fread() warnings with @ operator to avoid noise
- Simplified startOutputReader() since event loop polling is now
  handled directly in onOpen()
- Added null checks for event loop before using in timers
- Server now properly starts with continuous 100ms output polling

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-07 14:17:28 +08:00

40 lines
846 B
PHP

<?php
/**
* WebSocket 服务器测试脚本
*/
require_once __DIR__ . '/vendor/autoload.php';
use WebSocket\Client;
echo "连接到 WebSocket 服务器: ws://localhost:9002\n";
try {
$client = new Client("ws://localhost:9002");
echo "[成功] 连接到服务器\n";
// 接收欢迎消息
$message = $client->receive();
echo "[收到] " . $message . "\n";
// 发送测试输入
echo "[发送] 测试命令\n";
$client->send(json_encode([
'type' => 'input',
'input' => '1'
]));
// 接收响应
for ($i = 0; $i < 5; $i++) {
$message = $client->receive();
echo "[收到] " . substr($message, 0, 100) . "...\n";
}
$client->close();
echo "[完成] 测试成功\n";
} catch (Exception $e) {
echo "[错误] " . $e->getMessage() . "\n";
exit(1);
}