码迷,mamicode.com
首页 > Web开发 > 详细

Swoole实现简单的http服务器

时间:2020-09-17 16:57:29      阅读:37      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!