<?PHP /** * Created by PHPStorm. * User: mac * Date: 2019/9/13 * Time: 21:00 */ class HttpServer { public $http_serv; const PORT = 9501; const IP = "0.0.0.0"; public $static_handel_switch = true; public $document_root = '/www/swoole'; public function __construct() { $this->http_serv = new Swoole\Http\Server(self::IP,self::PORT); $this->http_serv->on("request",[$this,"onRequest"]); if($this->static_handel_switch == true){ $this->enableStaticHandel($this->document_root); } } /** * 接受到客户端请求 * @param $request 请求 * @param $response 响应 */ public function onRequest($request,$response) { //var_dump($request->server['request_uri']); if($request->server['request_uri'] != "/favicon.ico"){ $headers = [ 'Content-Type'=>"text/html; charset=utf-8" ]; $this->setHeader($headers,$response); $response->end("<h1>hello swoole ".rand(100,999)."</h1>"); } } public function setHeader(array $headers ,$response) { foreach($headers as $key=>$header){ $response->header($key,$header); } } /** * 启动 */ public function start() { $this->http_serv->start(); } public function enableStaticHandel($document_root) { $this->http_serv->set( [ 'document_root' => $document_root, // v4.4.0以下版本, 此处必须为绝对路径 'enable_static_handler' => true, ] ); } } $http = new HttpServer(); $http->start();
cli执行
浏览器访问
http://192.168.1.200:9501 ip为自己虚拟机ip
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。