老师,这用类的方式写开启服务后没有输出

来源:7-14 登录模块优化(一)

刘小桓

2018-12-01

<?php
/**
* http server.
* User: Administrator
* Date: 2018/12/1/001
* Time: 10:46
*/
class Http
{
   CONST HOST = '0.0.0.0';
   CONST PORT = 9508;

   public $http = null;

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

       $this->http->set(
           [
               'enable_static_handler'=>true,
               'document_root'=>'/www/wwwroot/app01/public/static',
               'worker_num'=>4,
               'task_worker_num'=>4,
           ]
       );

       $this->http->on('workerstart',[$this,'onWorkerStart']);
       $this->http->on('request',[$this,'onRequest']);
       $this->http->on('task',[$this,'onTask']);
       $this->http->on('finish',[$this,'onFinish']);
       $this->http->on('close',[$this,'onClose']);

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

   /**
    * @param $server
    * @param $worker_id
    */
   public function onWorkerStart($server,$worker_id)
   {
       //    define('APP_PATH',__DIR__.'/../application/');
       require __DIR__ . '/../thinkphp/base.php';

   }

   /**
    * @param $request
    * @param $response
    */
   public function onRequest($request,$response)
   {
       $_SERVER = [];
       if(isset($request->server)){
           foreach ($request->server as $k=>$v){
               $_SERVER[strtoupper($k)] = $v;
           }
       }

       if (isset($request->header)){
           foreach ($request->header as $k=>$v){
               $_SERVER[strtoupper($k)] = $v;
           }
       }
       $_GET = [];
       if (isset($request->get)){
           foreach ($request->get as $k=>$v){
               $_GET[$k]=$v;
           }
       }

       $_POST = [];
       if (isset($request->post)){
           foreach ($request->post as $k=>$v){
               $_POST[$k] = $v;
           }
       }
       ob_start();
       // 执行并响应程序
       try{
           think\Container::get('app')->run()->send();
       }catch (\Exception $e){
           //to do
       }
       $res = ob_get_contents();
//        ob_end_clean();
       $response->end($res);
   }

   /**
    * @param $server
    * @param $taskId
    * @param $workerId
    * @param $data
    */
   public function onTask($server,$taskId,$workerId,$data)
   {

   }

   /**
    * @param $server
    * @param $taskId
    * @param $data
    */
   public function onFinish($server,$taskId,$data)
   {

   }


   public function onClose()
   {

   }
}
new Http();



写回答

1回答

singwa

2018-12-01

您好。终端又报错信息提示什么的吗?

0
2
singwa
回复
刘小桓
解决了就好。祝您学习愉快。
2018-12-13
共2条回复

Swoole入门到实战打造高性能赛事直播平台

swoole创始人推荐,借助Swoole打造高性能直播平台

2228 学习 · 1062 问题

查看课程