小白兔晒黑了

导航

 

视频 https://www.bilibili.com/video/BV14E411t7T4?p=23&vd_source=4a69745b599dffec877b0fcfe130b092

代码

\pro\boot

<?php

use Core\server\HttpServer;
use Swoole\Process;

require_once __DIR__.'/vendor/autoload.php';
if ($argc==2){
    $cmd = $argv[1];
    if ($cmd=='start'){
        $http = new HttpServer();
        $http->run();
    
    } elseif ($cmd=='stop'){
        $mid = intval(file_get_contents("./ttt.pid"));
        if (trim($mid)){
            Process::kill($mid);
        }
    } else {
        echo '无效命令';
    }
}   else {
    echo '无效命令';
}

 

\pro\core\server\HttpServer.php

<?php
namespace Core\server;
use Swoole\Http\Server;

class HttpServer
{
    private $server;
    
    /**
     * HttpServer constructor.
     *
     * @param $server
     */
    public function __construct()
    {
        $this->server  = new \Swoole\Http\Server('0.0.0.0',80);
        //配置参数 https://wiki.swoole.com/#/server/setting
        $this->server->set(array(
          'worker_num'=>1,     //设置启动的 Worker 进程数。【默认值:CPU 核数】
          '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']);
        //$http->start();
        
    }
    
    public function onRequset($req,$res)
    {
    
    }
    
    
    public function onStart(Server $server )
    {
        $mid =  $server->master_pid;   //返回当前服务器主进程的 PID。
        file_put_contents("./ttt.pid",$mid);  //会覆盖
    }
    
    public function onShutDown(Server $server)
    {
        echo '关闭了'.PHP_EOL;
        unlink("./ttt.pid");
    }
    
    public function run()
    {
        $this->server->start();
    }
}

测试

1 启动

 

 2 查看进程

 

 

3 关闭进程

 

posted on 2022-07-06 01:42  小白兔晒黑了  阅读(48)  评论(0编辑  收藏  举报