<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## **一,HTTP服務器創建** ~~~ <?php namespace server; class HttpServer { protected $serv = null; //Swoole\Server對象 protected $host = '0.0.0.0'; //監聽對應外網的IP 0.0.0.0監聽所有ip protected $port = 9603; //監聽端口號 public function __construct() { $this->serv = new \Swoole\Http\Server($this->host, $this->port); //設置參數 //如果業務代碼是全異步 IO 的,worker_num設置為 CPU 核數的 1-4 倍最合理 //如果業務代碼為同步 IO,worker_num需要根據請求響應時間和系統負載來調整,例如:100-500 //假設每個進程占用 40M 內存,100 個進程就需要占用 4G 內存 $this->serv->set(array( 'worker_num' => 4, //設置啟動的worker進程數。【默認值:CPU 核數】 'max_request' => 10000, //設置每個worker進程的最大任務數。【默認值:0 即不會退出進程】 'daemonize' => 0, //開啟守護進程化【默認值:0】 )); //監聽服務器啟動事件 $this->serv->on('start', function ($server) { echo "Swoole http server is started"; }); //監聽請求,HTTP服務器只需要關注請求響應即可 //當有新的 HTTP 請求進入就會觸發此事件 //$request 對象,包含了請求的相關信息 //$response 對象,對 request 的響應可以通過操作 response 對象來完成。 //$response->end() 方法表示輸出一段 HTML 內容,并結束此請求 $this->serv->on('request', function ($request, $response) { // 使用 Chrome 瀏覽器訪問服務器,會產生額外的一次請求,/favicon.ico,可以在代碼中響應 404 錯誤。 if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') { $response->end(); return; } //打印請求的header var_dump($request->header); //打印請求的get參數 var_dump($request->get); //輸出返回內容 $response->header("Content-Type", "text/html; charset=utf-8"); $response->end("我是Swoole HTTP服務器輸出的返回內容\n"); }); //啟動服務 $this->serv->start(); } } $httpServer = new HttpServer(); ~~~ ## **二,運行HTTP服務** > **需要用php CLI模式運行** > ![](https://img.kancloud.cn/a6/7e/a67e35fb7c32d05b045599dcd3b98dfa_997x243.png) ## **三,訪問HTTP服務** 瀏覽器訪問應用地址,會輸出HTTP服務返回的內容 ![](https://img.kancloud.cn/11/09/1109f0390c04b45081ec4b871ffa6de1_901x219.png) HTTP服務端會打印客戶端request的數據,并輸出響應內容 ![](https://img.kancloud.cn/95/0f/950f28439cab0cc29bce512a1a3416f7_1268x377.png) ## **四,URL路由** 應用程序可以根據`$request->server['request_uri']`實現路由。如:`http://najingquan.com:9603/test/index/?a=1`,代碼中可以這樣實現`URL`路由。 ~~~ <?php // +---------------------------------------------------------------------- // | najing [ 通用后臺管理系統 ] // +---------------------------------------------------------------------- // | Copyright (c) 2020 http://www.najingquan.com All rights reserved. // +---------------------------------------------------------------------- // | Author: 救火隊隊長 // +---------------------------------------------------------------------- namespace server; /** * 概要描述:HTTP服務器 * @author: 救火隊隊長 * @since: 2020-05-23 22:45 */ class HttpServer { protected $serv = null; //Swoole\Server對象 protected $host = '0.0.0.0'; //監聽對應外網的IP 0.0.0.0監聽所有ip protected $port = 9603; //監聽端口號 public function __construct() { $this->serv = new \Swoole\Http\Server($this->host, $this->port); //設置參數 //如果業務代碼是全異步 IO 的,worker_num設置為 CPU 核數的 1-4 倍最合理 //如果業務代碼為同步 IO,worker_num需要根據請求響應時間和系統負載來調整,例如:100-500 //假設每個進程占用 40M 內存,100 個進程就需要占用 4G 內存 $this->serv->set(array( 'worker_num' => 4, //設置啟動的worker進程數。【默認值:CPU 核數】 'max_request' => 10000, //設置每個worker進程的最大任務數。【默認值:0 即不會退出進程】 'daemonize' => 0, //開啟守護進程化【默認值:0】 )); //監聽服務器啟動事件 $this->serv->on('start', function ($server) { echo "Swoole http server is started"; }); //監聽請求,HTTP服務器只需要關注請求響應即可 //當有新的 HTTP 請求進入就會觸發此事件 //$request 對象,包含了請求的相關信息 //$response 對象,對 request 的響應可以通過操作 response 對象來完成。 //$response->end() 方法表示輸出一段 HTML 內容,并結束此請求 $this->serv->on('request', function ($request, $response) { // 使用 Chrome 瀏覽器訪問服務器,會產生額外的一次請求,/favicon.ico,可以在代碼中響應 404 錯誤。 if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') { $response->end(); return; } //打印請求的header var_dump($request->header); //打印請求的get參數 var_dump($request->get); //輸出返回內容 $response->header("Content-Type", "text/html; charset=utf-8"); list($controller, $action) = explode('/', trim($request->server['request_uri'], '/')); //根據 $controller, $action 映射到不同的控制器類和方法 // 控制器首字母大寫 $controller = ucfirst($controller); //判斷控制器類是否存在 if (file_exists(__DIR__.'/'.str_replace('\\', '/', $controller).'.php')) { require_once __DIR__.'/'.str_replace('\\', '/', $controller).'.php'; $obj= new $controller; //判斷控制器方法是否存在 if (!method_exists($obj, $action)) { $response->status(404); $response->end("<meta charset='UTF-8'>兄弟,方法不存在!"); } else { //如果存在此方法,輸出結果 $response->end($obj->$action($request)); } } else { $response->status(404); $response->end("<meta charset='UTF-8'>兄弟,方法不存在!"); } }); //啟動服務 $this->serv->start(); } } $httpServer = new HttpServer(); ~~~ ## **五、創建Test控制器** ~~~ <?php class Test { public function index($request) { //返回請求結果 return "<h1>Hello Swoole. #".rand(1000, 9999)."</h1>"; } } ~~~ 瀏覽器訪問應用地址,會輸出HTTP服務返回的內容 ![](https://img.kancloud.cn/89/9c/899c425a0e4eb461385cc76dde9f09cc_897x208.png) 請求不存在的方法時,會提示方法不存在 ![](https://img.kancloud.cn/57/dc/57dca96a03ef0d53d7a9d57927f01b03_653x158.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看