计算机网络/计算机科学与应用/系统/运维/开发

Swoole HTTP面向对象封装

<?php

 class HTTP{

        private $http = null;

        public function __construct(){

                $this->http = new Swoole\Http\Server('0.0.0.0', 9501);


                /*

                $this->http->set([

                        'worker_num'    => 4,     // worker process num

                        'max_request'   => 50,

                ]);

                */

                $this->http->on('request',[$this,"onRequest"]);

                $this->http->start();

        }




        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();



# php HTTP.php



如果出现无法访问情况,注意查看端口号开放

访问http://42.192.180.238:9501/

Hello Swoole. #9853


swoole php

人生活在得失之间,得亦是失,失亦是得。

评论

^