/** * @param string $event * @param callable $callback */ public function on($event, callable $callback) { /** * register request handler */ $this->server->on($event, $callback); }
public function __construct() { // 创建swoole_http_server对象 $http = new swoole_http_server("0.0.0.0", 9501); // 设置参数 $http->set(array('worker_num' => 16, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 1)); $http->setGlobal(HTTP_GLOBAL_ALL); // 绑定WorkerStart $http->on('WorkerStart', array($this, 'onWorkerStart')); // 绑定request $http->on('request', function ($request, $response) { ob_start(); try { //注入uri $_GET['_url'] = $request->server['request_uri']; $application = new \Phalcon\Mvc\Application($this->di); echo $application->handle()->getContent(); } catch (Exception $e) { echo $e->getMessage(); } $result = ob_get_contents(); ob_end_clean(); $response->end($result); }); $http->start(); }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 8059); $http->set(array('worker_num' => 1, 'daemonize' => false, 'max_request' => 5000, 'user' => 'apache', 'group' => 'apache')); $http->on('Start', array($this, 'onStart')); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('Request', array($this, 'onRequest')); $http->start(); }
public function start($host, $port) { $controller = $this->controller; $http = new \swoole_http_server($host, $port, SWOOLE_BASE); $http->on('request', array($this, 'onRequest')); $http->set(['worker_num' => 1]); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->start(); }
/** * {@inheritdoc} */ public function start(ServerContextInterface $context, RequestHandlerInterface $requestHandler) { $this->server = new \swoole_http_server($context->getListenAddress(), $context->getListenPort(), \SWOOLE_BASE); if (null !== $this->serverOptions) { $this->server->set($this->serverOptions->getOptions()); } $this->server->on('request', $this->createRequestHandler($requestHandler)); $this->server->start(); }
/** * 初始化 */ private function __construct($ip, $port) { register_shutdown_function(array($this, 'handleFatal')); $http = new \swoole_http_server($ip, $port); $http->set(\Config\Server::getWebServerConfig()); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', array($this, 'onRequest')); $http->on('start', array($this, 'onStart')); $http->start(); }
protected function createServer() { $server = new \swoole_http_server($this->host, $this->port); $server->on('start', [$this, 'onServerStart']); $server->on('shutdown', [$this, 'onServerStop']); $server->on('managerStart', [$this, 'onManagerStart']); $server->on('workerStart', [$this, 'onWorkerStart']); $server->on('workerStop', [$this, 'onWorkerStop']); $server->on('request', [$this, 'onRequest']); if (method_exists($this, 'onOpen')) { $server->on('open', [$this, 'onOpen']); } if (method_exists($this, 'onClose')) { $server->on('close', [$this, 'onClose']); } if (method_exists($this, 'onWsHandshake')) { $server->on('handshake', [$this, 'onWsHandshake']); } if (method_exists($this, 'onWsMessage')) { $server->on('message', [$this, 'onWsMessage']); } if (method_exists($this, 'onTask')) { $server->on('task', [$this, 'onTask']); } if (method_exists($this, 'onFinish')) { $server->on('finish', [$this, 'onFinish']); } $server->set($this->normalizedConfig()); return $server; }
/** * 初始化 */ private function __construct() { register_shutdown_function(array($this, 'handleFatal')); $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 12, 'daemonize' => false, 'max_request' => 128, 'dispatch_mode' => 1, 'log_file' => '/home/bweb/logs/swoole.log', 'open_tcp_nodelay' => true)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', array($this, 'onRequest')); $http->on('start', array($this, 'onStart')); self::getYii(); $http->start(); }
/** * 初始化 */ private function __construct() { define('CISWOOLE', TRUE); register_shutdown_function(array($this, 'handleFatal')); $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 8, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 1)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', array($this, 'onRequest')); $http->on('start', array($this, 'onStart')); $http->start(); }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 8080); $http->set(array('worker_num' => 16, 'max_conn' => 10000, 'max_request' => 5000, 'dispatch_mode' => 1, 'daemonize' => 1, 'log_file' => '/var/log/swoole.log')); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', function ($request, $response) { if (isset($request->server)) { HttpServer::$server = $request->server; } else { HttpServer::$server = []; } if (isset($request->header)) { HttpServer::$header = $request->header; } else { HttpServer::$header = []; } if (isset($request->get)) { HttpServer::$get = $request->get; } else { HttpServer::$get = []; } if (isset($request->post)) { HttpServer::$post = $request->post; } else { HttpServer::$post = []; } // TODO handle img ob_start(); try { //var_dump(HttpServer::$server['request_uri']); $_SERVER['REQUEST_URI'] = HttpServer::$server['request_uri']; //$_SERVER['REMOTE_ADDR'] = HttpServer::$server['request_uri']; $yaf_request = new Yaf_Request_Http(HttpServer::$server['request_uri']); $this->application->getDispatcher()->dispatch($yaf_request); // unset(Yaf_Application::app()); } catch (\Exception $e) { var_dump($e); } $result = ob_get_contents(); ob_end_clean(); // add Header $response->header('Content-Type', 'no-cache, must-revalidate'); $response->header('Content-Type', 'application/json'); //$response->header('Content-Encoding', 'gzip'); $response->header('Server', 'verycloud'); // add cookies // set status $response->end($result); }); $http->on('WorkerStop', array($this, 'onWorkerStop')); $http->start(); }
public function __construct() { // 创建swoole_http_server对象 $http = new swoole_http_server("0.0.0.0", 9502); // 设置参数 $http->set(array('worker_num' => 16, 'daemonize' => false, 'max_request' => 100000, 'dispatch_mode' => 1)); $http->setGlobal(HTTP_GLOBAL_ALL); // 绑定WorkerStart $http->on('WorkerStart', array($this, 'onWorkerStart')); // 绑定request $http->on('request', array($this, 'onRequest')); //开启服务器 $http->start(); }
public function __construct($laravelDir, $options, $kernelClass = '\\App\\Http\\Kernel') { $this->laravelDir = realpath($laravelDir); $this->compiledPath = $this->laravelDir . 'bootstrap/cache/compiled.php'; if (LOAD_COMPILED_BEFORE_WORKER) { $this->loadCompiledAndInitSth(); } $this->swoole_http_server = $server = new \swoole_http_server($options['listen_ip'], $options['listen_port']); $this->kernelClass = $kernelClass; $server->set($options); $server->on('WorkerStart', array($this, 'onWorkerStart')); $server->on('request', array($this, 'onRequest')); return $this; }
public function onInit() { $host = isset($this->_swoole_cfg['swoole']['swoole_host']) ? $this->_swoole_cfg['swoole']['swoole_host'] : '0.0.0.0'; $port = isset($this->_swoole_cfg['swoole']['swoole_port']) ? $this->_swoole_cfg['swoole']['swoole_port'] : 80; $serv = new swoole_http_server($host, $port); /** * 这里对swoole进程进行设置 */ $serv->set($this->_getSwooleCoreCfg()); $serv->on('WorkerStart', array($this, 'onWorkerStart')); $serv->on('WorkerStop', array($this, 'onWorkerStop')); $serv->on('Request', array($this, 'onRequest')); $serv->on('Task', array($this, 'onTask')); $serv->on('Finish', array($this, 'onFinish')); $this->_serv = $serv; }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 16, 'daemonize' => true, 'max_request' => 10000, 'dispatch_mode' => 1)); $http->on('Request', array($this, 'onRequest')); $http->start(); }
/** * * @param string $host * @param int $port * @param array $config */ public function start() { $server = new \swoole_http_server($this->host, $this->port); $server->set($this->config); $server->on('Request', array($this, 'onRequest')); $server->start(); }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 50, 'daemonize' => true, 'max_request' => 10000, 'dispatch_mode' => 16)); $http->setGlobal(HTTP_GLOBAL_ALL); $http->on('Start', function () { if (function_exists('cli_set_process_title')) { cli_set_process_title('app_server'); } else { if (function_exists('swoole_set_process_name')) { swoole_set_process_name('app_server'); } } }); $http->on('Request', array($this, 'onRequest')); $http->start(); }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 8, 'daemonize' => false, 'max_request' => 1000, 'dispatch_mode' => 1)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', function ($request, $response) { if (isset($request->server)) { HttpServer::$server = $request->server; foreach ($request->server as $key => $value) { $_SERVER[strtoupper($key)] = $value; } } if (isset($request->header)) { HttpServer::$header = $request->header; } if (isset($request->get)) { HttpServer::$get = $request->get; foreach ($request->get as $key => $value) { $_GET[$key] = $value; } } if (isset($request->post)) { HttpServer::$post = $request->post; foreach ($request->post as $key => $value) { $_POST[$key] = $value; } } if (isset($request->request_uri)) { $_SERVER['REQUEST_URI'] = $request->request_uri; } ob_start(); //实例化ycf对象 try { Ycf\Core\YcfCore::run(); } catch (Exception $e) { var_dump($e); } $result = ob_get_contents(); ob_end_clean(); $response->end($result); unset($result); }); $http->start(); }
public function __construct() { $http = new swoole_http_server("127.0.0.1", 9501); $http->set(array('worker_num' => 16, 'daemonize' => true, 'max_request' => 10000, 'dispatch_mode' => 1)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', function ($request, $response) { if (isset($request->server)) { HttpServer::$server = $request->server; } else { HttpServer::$server = []; } if (isset($request->header)) { HttpServer::$header = $request->header; } else { HttpServer::$header = []; } if (isset($request->get)) { HttpServer::$get = $request->get; } else { HttpServer::$get = []; } if (isset($request->post)) { HttpServer::$post = $request->post; } else { HttpServer::$post = []; } // TODO handle img ob_start(); try { $yaf_request = new Yaf\Request_Http(HttpServer::$server['request_uri']); $this->application->getDispatcher()->dispatch($yaf_request); // unset(Yaf_Application::app()); } catch (Yaf_Exception $e) { var_dump($e); } $result = ob_get_contents(); ob_end_clean(); // add Header // add cookies // set status $response->end($result); }); $http->start(); }
public function __construct($ip, $port) { parent::__construct(); $this->server = $server = new \swoole_http_server($ip, $port); $server->on('Request', function ($request, $response) { $request = new \Owl\Swoole\Request($request); $response = new \Owl\Swoole\Response($response); $this->execute($request, $response); }); }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 10, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 1)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', function ($request, $response) { if (isset($request->server)) { HttpServer::$server = $request->server; foreach ($request->server as $key => $value) { $_SERVER[strtoupper($key)] = $value; } } if (isset($request->header)) { HttpServer::$header = $request->header; } if (isset($request->get)) { HttpServer::$get = $request->get; foreach ($request->get as $key => $value) { $_GET[$key] = $value; } } if (isset($request->post)) { HttpServer::$post = $request->post; foreach ($request->post as $key => $value) { $_POST[$key] = $value; } } ob_start(); //实例化yii对象 try { $this->application = Yii::createWebApplication(FRAMEWORK_CONFIG); $this->application->run(); } catch (Exception $e) { var_dump($e); } $result = ob_get_contents(); ob_end_clean(); $response->end($result); unset($result); unset($this->application); }); $http->start(); }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 16, 'daemonize' => true, 'max_request' => 10000, 'dispatch_mode' => 1)); define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application"); $this->application = new Yaf_Application(dirname(APPLICATION_PATH) . "/conf/application.ini"); $this->application->bootstrap(); $http->on('Request', array($this, 'onRequest')); $http->start(); }
public function __construct() { $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 16, 'daemonize' => false, 'max_request' => 10000, 'dispatch_mode' => 1)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', function ($request, $response) { if (isset($request->server)) { SwooleHttpServer::$server = $request->server; foreach ($request->server as $key => $value) { $_SERVER[strtoupper($key)] = $value; } } if (isset($request->header)) { SwooleHttpServer::$header = $request->header; } if (isset($request->get)) { SwooleHttpServer::$get = $request->get; foreach ($request->get as $key => $value) { $_GET[$key] = $value; } } if (isset($request->post)) { SwooleHttpServer::$post = $request->post; foreach ($request->post as $key => $value) { $_POST[$key] = $value; } } /* $uri = explode( "?", $_SERVER['REQUEST_URI'] ); $_SERVER["PATH_INFO"] = $uri[0]; if (isset($uri[1])) { $_SERVER['QUERY_STRING'] = $uri[1]; }*/ $_SERVER['argv'][1] = $_SERVER["PATH_INFO"]; ob_start(); require_once '../../../ThinkPHP/ThinkPHP.php'; $result = ob_get_contents(); ob_end_clean(); $response->end($result); }); $http->start(); }
function run($_setting = array()) { $default_setting = array('worker_num' => 4); $this->pid_file = $_setting['pid_file']; $setting = array_merge($default_setting, $_setting); $serv = new \swoole_http_server('0.0.0.0', self::PORT, SWOOLE_PROCESS); $serv->set($setting); $serv->on('Request', array($this, 'onRequest')); $this->serv = $serv; $this->serv->start(); }
protected function execute(InputInterface $input, OutputInterface $output) { $kernel = $this->getContainer()->get('kernel'); $http = new \swoole_http_server($input->getOption('host'), $input->getOption('port')); $http->on('request', function ($req, $res) use($kernel, $http) { $symfonyRequest = Request::createSymfonyRequest($req); $symfonyResponse = $kernel->handle($symfonyRequest); Response::send($res, $symfonyResponse); $kernel->terminate($symfonyRequest, $symfonyResponse); }); $http->start(); }
public function __construct() { $http = new swoole_http_server('0.0.0.0', 9502); $http->set(array('worker_num' => 4, 'daemonize' => 0, 'dispatch_mode' => 1)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', function ($request, $response) { if (isset($request->server)) { HttpServer::$server = $request->server; } else { HttpServer::$server = []; } if (isset($request->header)) { HttpServer::$header = $request->header; } else { HttpServer::$header = []; } if (isset($request->get)) { HttpServer::$get = $request->get; } else { HttpServer::$get = []; } if (isset($request->post)) { HttpServer::$post = $request->post; } else { HttpServer::$post = []; } ob_start(); try { $yaf_request = new Yaf_Request_Http(HttpServer::$server['request_uri']); $this->application->getDispatcher()->dispatch($yaf_request); } catch (Yaf_Exception $e) { var_dump($e); } $result = ob_get_contents(); ob_end_clean(); $response->end($result); }); $http->start(); }
/** * @param App $app * @param array $serverSettings */ public function __construct(App $app, array $serverSettings = []) { $app->getContainer()['http.server.config'] = $serverSettings; $app->getContainer()['swoole.http.server'] = function ($container) { $httpServer = new \swoole_http_server($this->host, $this->port); $httpServer->set($container['http.server.config']); $httpServer->setGlobal(HTTP_GLOBAL_ALL); $httpServer->on('request', $container['http.request.handler']); return $httpServer; }; $app->getContainer()['http.request.handler'] = function () use($app) { return new RequestHandler($app); }; $this->app = $app; }
public function run() { if (isset($this->on['workerstart'])) { $this->workerstart = $this->on['workerstart']; } $serv = new swoole_http_server(SWOOLE_HOST, SWOOLE_PORT); $serv->set($this->set); $this->on['start'] = array($this, 'onStart'); //避免onStart被重写 $this->on['request'] = array($this, 'onRequest'); //避免Request被重写 $this->on['shutdown'] = array($this, 'onShutdown'); //避免Shutdown被重写 $this->on['workerstart'] = array($this, 'onWorkerStart'); //避免Workerstart被重写 foreach ($this->on as $k => $v) { $serv->on($k, $v); } $serv->start(); }
$http->on('request', function ($request, $response) { $response->end('ok'); if (false !== strpos($request->server['path_info'], 'reload')) { global $http; $http->reload(); return; } if (empty($request->post['hook'])) { mylog('hook data empty'); return; } $data = json_decode($request->post['hook'], true); if (empty($data)) { //错误的数据 mylog('hook no json data'); return; } if (PASSWORD && $data['password'] != PASSWORD) { //密码不匹配 mylog('password error'); return; } if (empty($data['push_data']['commits'])) { //没有提交者 mylog('no commit'); return; } //项目名称 $project = $data['push_data']['repository']['name']; //分支名称 $ref = $data['push_data']['ref']; mylog("{$project}: {$ref} push"); global $config; if (empty($config[$project][$ref])) { mylog("no {$project} {$ref}"); return; } $client = new \swoole_client(SWOOLE_SOCK_UDP); foreach ($config[$project][$ref] as $item) { $client->connect($item['host'], $item['port']); $client->send(json_encode([$project, $ref, $item['path']])); } });
<?php /** * Created by PhpStorm. * User: janhuang * Date: 15/12/8 * Time: 下午12:00 * Github: https://www.github.com/janhuang * Coding: https://www.coding.net/janhuang * SegmentFault: http://segmentfault.com/u/janhuang * Blog: http://segmentfault.com/blog/janhuang * Gmail: bboyjanhuang@gmail.com * WebSite: http://www.janhuang.me */ include __DIR__ . '/../vendor/autoload.php'; $server = new \swoole_http_server('0.0.0.0', 9600); $server->on('request', function ($request, $response) { $document_root = __DIR__; $script_name = 'swoole.php'; print_r($request); $request = \FastD\Http\SwooleRequest::createRequestHandle($request, ['document_root' => $document_root, 'script_name' => $script_name]); print_r($request); $response->write('base : ' . $request->getBaseUrl() . '<br />'); $response->write('request_uri: ' . $request->getRequestUri() . '<br />'); $response->write('path_info: ' . $request->getPathInfo()); $response->end(''); }); $server->start();
public function __construct() { $http = new swoole_http_server("0.0.0.0", 9501); $http->set(array('worker_num' => 16, 'daemonize' => 0, 'max_request' => 10000, 'dispatch_mode' => 1)); $http->on('WorkerStart', array($this, 'onWorkerStart')); $http->on('request', function ($request, $response) { if (isset($request->server)) { foreach ($request->server as $key => $value) { unset($_SERVER[strtoupper($key)]); $_SERVER[strtoupper($key)] = $value; } } if (isset($request->header)) { foreach ($request->header as $key => $value) { unset($_SERVER[strtoupper($key)]); $_SERVER[strtoupper($key)] = $value; } } unset($_GET); if (isset($request->get)) { foreach ($request->get as $key => $value) { $_GET[$key] = $value; } } unset($_POST); if (isset($request->post)) { foreach ($request->post as $key => $value) { $_POST[$key] = $value; } } unset($_COOKIE); if (isset($request->cookie)) { foreach ($request->cookie as $key => $value) { $_COOKIE[$key] = $value; } } unset($_FILES); if (isset($request->files)) { foreach ($request->files as $key => $value) { $_FILES[$key] = $value; } } /* $uri = explode( "?", $_SERVER['REQUEST_URI'] ); $_SERVER["PATH_INFO"] = $uri[0]; if( isset( $uri[1] ) ) { $_SERVER['QUERY_STRING'] = $uri[1]; }*/ $_SERVER["PATH_INFO"] = explode('/', $_SERVER["PATH_INFO"], 3)[2]; $_SERVER['argv'][1] = $_SERVER["PATH_INFO"]; ob_start(); // 记录加载文件时间 G('loadTime'); // 运行应用 \Think\App::run(); $result = ob_get_contents(); ob_end_clean(); $response->end($result); }); $http->start(); }