swoole--HTTP

1:因为是HTTP协议 开启服务后请求客户端HTML文件,直接浏览器URL,先创服务端文件HTTP.php

<?php

class HTTP
{
    private $server = null;
    
    public function __construct(){
        $this -> server = new Swoole\Http\Server('0.0.0.0', 9501);
        $this -> server -> set([
            "enable_static_handler"    => true,
            "document_root"   => "/www/swoole/static",//文件位置
        ]);
        $this -> server -> on('Request', [$this, "onRequest"]);
        $this -> server -> start();
    }
    /**
     * $fd  客户端链进来的id:0/开始增长
     * 
    */
    public function onRequest($request, $response){
        $response->header('Content-Type', 'text/html; charset=utf-8');
        $response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
    }
}

new HTTP();

 

2:启动服务php HTTP.php

posted @ 2021-12-02 10:40  用代码砌墙的小白  阅读(90)  评论(0编辑  收藏  举报