SwooleHttpServer

	<?php
class SwooleHttp{
    private $httpd;

    public function __construct(){
        $this->httpd = new swoole_http_server("192.168.43.91", 80);
        $this->httpd->on("Start", [$this, "onMasterStart"]);
        $this->httpd->on("ManagerStart", [$this, "onManagerStart"]);
        $this->httpd->on("workerStart", [$this, "onWokerStart"]);
        $this->httpd->on("request", [$this, "onRequest"]);

        $this->httpd->set([
            "worker_num" => 8
        ]);

        $this->httpd->start();
    }

    public function onRequest($request, $response){
        $path_info = explode("/", $request->server["path_info"]);
        var_dump($path_info);
//        var_dump($request->server["path_info"]);
        if(isset($path_info[1]) && trim($path_info[1]) != ""){
            if($path_info[1] == "favicon.ico"){

                $fp = fopen("./favicon.ico", "rb");
                while(!feof($fp)){
                    $buffer = fread($fp, 1024);
                }
               $response->end($buffer);
               return true;
            }
            $controller = "controllers\\".ucfirst($path_info[1])."Controller";
        }else{
            $controller = "controllers\\"."IndexController";
        }

        if(isset($path_info[2]) && trim($path_info[2]) != ""){
            $action = "action".ucfirst($path_info[2]);
        }else{
            $action = "actionIndex";
        }

        $result = "page not found \n";
//        $response->end("Hellow Httpd \n");

        var_dump($controller);
        $controllerClass = new $controller();
        var_dump($controllerClass);
        if(class_exists($controller)){
//            echo "$controller is exist \n";
            $controllerClass = new $controller();
            if(method_exists($controllerClass, $action)){
                $result = $controllerClass->$action($request);
            }else{
                $response->end($result);
            }
        }else{
            $response->end($result);
        }

        $response->end($result);
    }

    public function onMasterStart(){
       swoole_set_process_name("SwooleMaster");
    }

    public function onManagerStart(){
        swoole_set_process_name("SwooleManager");
    }

    public function onWokerStart(){
        swoole_set_process_name("SwooleWorker");
        spl_autoload_register([$this, "onRequire"]);

    }

    public function onRequire($class){
        $base_dir = __DIR__;
        $namespace_path__to_file_path = str_replace("\\", "/", $class);
        $class_path = $base_dir."/".$namespace_path__to_file_path.".php";
        var_dump($class_path);
        if(is_file($class_path)){
            require $class_path;
        }
    }

}

new SwooleHttp();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值