feat(examples): 添加 Swoole 相关示例代码

- 新增 Swoole 协程运行示例文件 co.php
- 添加错误常量引用示例文件 const-ref.php
- 创建 Swoole HTTP 服务器示例文件 http.php
- 实现基本的协程运行功能
- 集成 HTTP 服务器请求处理逻辑
- 添加异常处理和响应状态管理
pull/1/head
韩天峰 4 months ago
parent 7519c3c523
commit 2d0baeaea9
  1. 5
      examples/error/const-ref.php
  2. 8
      examples/swoole/co.php
  3. 18
      examples/swoole/http.php

@ -0,0 +1,5 @@
<?php
function main()
{
parse_str('hello=s', "hello");
}

@ -0,0 +1,8 @@
<?php
function main(): void
{
Swoole\Coroutine\run(function () {
sleep(1);
});
}

@ -0,0 +1,18 @@
<?php
function main()
{
$http = new Swoole\Http\Server('127.0.0.1', 9501);
$http->on('request', function ($request, $response) {
try {
$response->header('Content-Type', 'text/plain');
$response->end('Hello World');
} catch (Exception $e) {
$response->status(500);
$response->end('error: ' . $e->getMessage());
}
});
$http->start();
}
Loading…
Cancel
Save