Swoole实现简单的http服务器

2021-01-04 06:28

阅读:476

标签:http服务   lang   response   protect   echo   sel   amp   and   stat   

class http_server
{
    const HOST = "127.0.0.1";
    const PORT = 8811;

    protected $server = null;


    public function __construct()
    {
        $this->server = new swoole_http_server(self::HOST, self::PORT);


        $this->server->set([
            "enable_static_handler" => true,
            "document_root" => "./public"
        ]);


        $this->server->on("request", [$this, "OnRequest"]);


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


    public function OnRequest($request, $response)
    {
        $requestParametersFormClient = json_encode($request->get);
        //format: http://127.0.0.1:8811/?m=1&n=2&o=3   =>  {"m":"1","n":"2","o":"3"}
        echo "request parameters from server is: " . $requestParametersFormClient . "\n";
        $response->end("ended!");
    }
}

new http_server();

Swoole实现简单的http服务器

标签:http服务   lang   response   protect   echo   sel   amp   and   stat   

原文地址:https://www.cnblogs.com/ezhar/p/13625359.html


评论


亲,登录后才可以留言!