swoole-framwork/core/server/HttpServer.php

92 lines
3.2 KiB
PHP
Raw Normal View History

2024-05-20 11:51:07 +08:00
<?php
namespace Core\server;
use Swoole\Http\Server;
use Core\init\TestProcess;
2024-05-31 14:57:01 +08:00
use Core\BeanFactory;
#[Injectable(lazy: true)]
2024-05-20 11:51:07 +08:00
class HttpServer
{
private $server;
private $dispatcher;
2024-05-31 14:57:01 +08:00
private $host = 'localhost';
private $port = 8086;
public function __construct()
2024-05-20 11:51:07 +08:00
{
2024-05-31 14:57:01 +08:00
$this->server = new Server($this->host,$this->port);
echo 'host: http://' . $this->host . ':' . $this->port . PHP_EOL;
2024-05-20 11:51:07 +08:00
//配置参数 https://wiki.swoole.com/#/server/setting
$this->server->set(array(
2024-05-23 10:51:16 +08:00
'worker_num' => 2, //设置启动的 Worker 进程数。【默认值CPU 核数】
2024-05-20 11:51:07 +08:00
'daemonize' => false // 使用docker 不需要设置守护进程
));
//$this->server->on('request',function ($req,$res){});
$this->server->on('Request', [$this, 'onRequset']);
$this->server->on('Start', [$this, 'onStart']);
$this->server->on('ShutDown', [$this, 'onShutDown']);
$this->server->on('WorkerStart',[$this,'onWorkerStart']);
2024-05-22 14:49:06 +08:00
$this->server->on('ManagerStart', [$this,"onManagerStart"]);
2024-05-20 11:51:07 +08:00
}
public function onWorkerStart(Server $server,int $workerId)
{
2024-05-22 14:49:06 +08:00
//cli_set_process_title('baihand worker');//设置进程名称
2024-05-20 11:51:07 +08:00
\Core\BeanFactory::init();
$this->dispatcher = \Core\BeanFactory::getBean('RouterCollector')->getDispatcher();
}
2024-05-22 14:49:06 +08:00
public function onManagerStart(Server $server)
{
//cli_set_process_title('baihand manger');
}
2024-05-20 11:51:07 +08:00
public function onStart(Server $server)
{
2024-05-22 14:49:06 +08:00
//cli_set_process_title('baihand manger');
2024-05-20 11:51:07 +08:00
$mid = $server->master_pid; //返回当前服务器主进程的 PID。
2024-05-21 14:33:56 +08:00
file_put_contents("./Buddha.pid", $mid); //会覆盖
2024-05-20 11:51:07 +08:00
}
public function onRequset($request, $response)
{
$myRequest = \Core\http\Request::init($request);
$myResponse = \Core\http\Response::init($response);
$routeInfo = $this->dispatcher->dispatch($myRequest->getMethod(),$myRequest->getUri());
switch($routeInfo[0]) {
case \FastRoute\Dispatcher::NOT_FOUND:
$response->status(404);
2024-05-31 14:57:01 +08:00
$response->write(json_encode(['code' => 404, 'msg' => 'NOT_FOUND']));
2024-05-20 11:51:07 +08:00
$response->end();
break;
case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$response->status(405);
2024-05-31 14:57:01 +08:00
$response->write(json_encode(['code' => 405, 'msg' => 'METHOD_NOT_ALLOWED']));
2024-05-20 11:51:07 +08:00
$response->end();
break;
case \FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];//参数
$extVars = [$myRequest, $myResponse];
//$response->end($handler($vars, $extVars));
$myResponse->setBody($handler($vars,$extVars));
$myResponse->end();
break;
}
}
public function onShutDown(Server $server)
{
echo '关闭了' . PHP_EOL;
2024-05-21 14:33:56 +08:00
unlink("./Buddha.pid");
2024-05-20 11:51:07 +08:00
}
public function run()
{
2024-05-22 14:49:06 +08:00
//$p = new TestProcess();
2024-05-20 11:51:07 +08:00
//$this->server->addProcess($p->run());
2024-05-21 14:33:56 +08:00
echo '就要启动了喂......' . PHP_EOL;
2024-05-20 11:51:07 +08:00
$this->server->start();
}
}