diff --git a/src/libs/MyWebSocketHandler.php b/src/libs/MyWebSocketHandler.php new file mode 100644 index 0000000..53aa708 --- /dev/null +++ b/src/libs/MyWebSocketHandler.php @@ -0,0 +1,24 @@ +send('Hello ' . $data); + } + + public function onClose(TcpConnection $connection): void + { + echo "Connection closed\n"; + } +} \ No newline at end of file diff --git a/src/start.php b/src/start.php index aeaeeaa..8b2ee36 100644 --- a/src/start.php +++ b/src/start.php @@ -1,27 +1,21 @@ onConnect = function ($connection) { - echo "New connection\n"; -}; +$ws_worker->onConnect = [$handler, 'onConnect']; +$ws_worker->onMessage = [$handler, 'onMessage']; +$ws_worker->onClose = [$handler, 'onClose']; -// Emitted when data received -$ws_worker->onMessage = function ($connection, $data) { - // Send hello $data - $connection->send('Hello ' . $data); -}; - -// Emitted when connection closed -$ws_worker->onClose = function ($connection) { - echo "Connection closed\n"; -}; - -// Run worker -Worker::runAll(); \ No newline at end of file +// 仅在非测试环境下运行 +if (!getenv('PHPUNIT_RUNNING')) { + Worker::runAll(); +} \ No newline at end of file diff --git a/src/workerman.log b/src/workerman.log new file mode 100644 index 0000000..a652dec --- /dev/null +++ b/src/workerman.log @@ -0,0 +1,8 @@ +2025-06-03 14:48:47 pid:6105 Workerman[start.php] start in DEBUG mode +2025-06-03 14:48:49 pid:6105 Workerman[start.php] received signal SIGINT +2025-06-03 14:48:49 pid:6105 Workerman[start.php] stopping +2025-06-03 14:48:49 pid:6105 Workerman[start.php] has been stopped +2025-06-03 15:11:55 pid:6213 Workerman[start.php] start in DEBUG mode +2025-06-03 15:11:56 pid:6213 Workerman[start.php] received signal SIGINT +2025-06-03 15:11:56 pid:6213 Workerman[start.php] stopping +2025-06-03 15:11:56 pid:6213 Workerman[start.php] has been stopped diff --git a/tests/CalculatorTest.php b/tests/Test.php similarity index 100% rename from tests/CalculatorTest.php rename to tests/Test.php