function handlerServer(Swoole\Request $request) { $response = new Swoole\Response(); $request->setGlobal(); //处理静态请求 if (!empty($this->server->config['apps']['do_static']) and $this->server->doStaticRequest($request, $response)) { return $response; } $php = Swoole::getInstance(); //将对象赋值到控制器 $php->request = $request; $php->response = $response; try { ob_start(); /*---------------------处理MVC----------------------*/ $response->body = $php->runMVC(); $response->body .= ob_get_contents(); ob_end_clean(); } catch (\Exception $e) { if ($request->finish != 1) { $this->server->httpError(404, $response, $e->getMessage()); } } //重定向 if (isset($response->head['Location'])) { $response->setHttpStatus(301); } return $response; }
/** * 处理请求 * @param $request * @return Swoole\Response */ public function onRequest(Swoole\Request $request) { if ('/filter' == $request->meta['path']) { $response = new Swoole\Response(); $this->currentResponse = $response; //get param word $word = self::getParam($request, 'word'); if ($word) { $result = $this->search($word); // $result = self::changeCharset($result,'gbk','utf-8'); $result = json_encode($result); $this->httpSend(200, $response, $result); return $response; } else { $this->httpSend(200, $response, "parameters word no found"); return $response; } } else { return parent::onRequest($request); } }
<?php define('DEBUG', 'on'); define("WEBPATH", realpath(__DIR__ . '/../')); require_once __DIR__ . '/../vendor/autoload.php'; Swoole\Loader::vendor_init(); Swoole\Loader::addNameSpace('WebIM', __DIR__ . '/src/'); Swoole::$php->config->setPath(__DIR__ . '/apps/configs'); $AppSvr = new Swoole\Protocol\HttpServer(); $AppSvr->loadSetting(__DIR__ . '/../swoole.ini'); //加载配置文件 $AppSvr->setDocumentRoot(__DIR__); $AppSvr->setLogger(new \Swoole\Log\EchoLog(true)); //Logger $server = new \Swoole\Network\Server('0.0.0.0', 8888); $server->setProtocol($AppSvr); $server->run(array('worker_num' => 1));
<?php define('DEBUG', 'off'); define("WEBPATH", realpath(__DIR__)); if (!(include __DIR__ . '/vendor/autoload.php')) { $message = <<<EOF <p>You must set up the project dependencies by running the following commands:</p> <pre> curl -s http://getcomposer.org/installer | php php composer.phar install </pre> EOF; if (PHP_SAPI === 'cli') { $message = strip_tags($message); } die($message); } $AppSvr = new Swoole\Protocol\HttpServer(); $AppSvr->loadSetting(__DIR__ . '/conf/swoole.ini'); //加载配置文件 //$AppSvr->setDocumentRoot(__DIR__.'/webroot'); $AppSvr->setLogger(new Swoole\Log\EchoLog(true)); //Logger $server = Swoole\Network\Server::autoCreate('0.0.0.0', 8888); $server->setProtocol($AppSvr); //$server->daemonize(); //作为守护进程 $server->run(array('worker_num' => 4, 'max_request' => 10000, 'log_file' => '/tmp/swoole.log'));