<?php
namespace app\server;
class http_server
{
private $server = null;
public function __construct()
{
$this->server = new \Swoole\Http\Server("0.0.0.0", 9501);
$this->server->set([
'worker_num' => 5,
'enable_static_handler' => true,
'document_root' => '/wwwroot/tp5.1/public/static',
]);
$this->server->on('WorkerStart', [$this, 'onWorkerStart']);
$this->server->on('request', [$this, 'onRequest']);
$this->server->start();
}
public function onWorkerStart($server, $worker_id)
{
// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
//加载框架引导文件
require __DIR__ . '/../thinkphp/base.php';
}
public function onRequest($request, $response)
{
print_r($request->server);
$_SERVER = [];
if (isset($request->server)) {
foreach ($request->server as $k => $v) {
$_SERVER[strtoupper($k)] = $v;
}
}
if (isset($request->header)) {
foreach ($request->header as $k => $v) {
$_SERVER[strtoupper($k)] = $v;
}
}
$_GET = [];
if (isset($request->get)) {
foreach ($request->get as $k => $v) {
$_GET[$k] = $v;
}
}
$_POST = [];
if (isset($request->post)) {
foreach ($request->post as $k => $v) {
$_POST[$k] = $v;
}
}
ob_start();
try {
// 执行应用并响应
\think\Container::get('app', [APP_PATH])->run()->send();
//\think\Container::get('app')->run()->send();
} catch (\Exception $e) {
echo $e->getMessage();
}
echo "--action--".request()->action().PHP_EOL;#request()->action()完全是thinkphp5.1内置方法
$response->header('content-type','text/html; charset=UTF-8');
$res = ob_get_contents();#获取缓存数据
ob_end_clean();
$response->end($res);
}
}
new http_server();