• Swoole实战之手撸HttpServer框架 15 初步封装我们的HttpServer启动代码


    视频 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 关闭进程

  • 相关阅读:
    MySQL表的四种分区类型
    微信开发配置(Yii框架下的开发)
    一道编程题—输出字符串内重复的数字
    无序数组内查找指定值(快速查找)
    指针
    chmod
    cookie和session的区别
    使用keytool生成证书
    人大金仓修改最大连接数
    数据库链接地址
  • 原文地址:https://www.cnblogs.com/polax/p/16449248.html
Copyright © 2020-2023  润新知