<?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();
SwooleHttpServer
最新推荐文章于 2024-05-08 08:32:31 发布