public static function getInstance() { if (!self::$instance) { self::$instance = new HttpServer(); } return self::$instance; }
/** * Method getInstance * @desc 获取对象 * @author WenJun <*****@*****.**> * @static * @return HttpServer|null */ public static function getInstance() { if (empty(self::$instance) || !self::$instance instanceof HttpServer) { self::$instance = new self(); } return self::$instance; }
$response->header('Accept-Charset', 'utf-8'); $response->header('Content-Type', 'application/vnd.collection+json'); // logic layer $res_arr = static::hotLogic($request, $this->json_col, $this->getRedisIns()); if (array_key_exists('code', $res_arr)) { $code = $res_arr['code']; $response->status($code); echo 'StCode:' . $code . PHP_EOL; if (201 == $code) { //created extra header $response->header('Location', Json_Collection::$host . '/' . $res_arr['location']); } } else { // 200 is the default code $this->json_col->fillWithArr($res_arr['arr']); $ret_str = $this->json_col->getEncodedStr(); $this->json_col->cleanUp(); } $httpcontent = isset($ret_str) ? $ret_str : ''; unset($ret_str); echo 'Ret_Cont:' . $httpcontent . PHP_EOL; $response->end($httpcontent); } public function run() { $this->serv->start(); } } $http = new HttpServer(); $http->setting(); $http->run();
/** * @param \Icicle\Http\Server\RequestHandler $handler * @param \Icicle\Log\Log|null $log * @param \Icicle\WebSocket\Protocol\ProtocolMatcher|null $matcher * @param \Icicle\WebSocket\Driver\Driver|null $driver * @param \Icicle\Socket\Server\ServerFactory|null $factory */ public function __construct(RequestHandler $handler, Log $log = null, ProtocolMatcher $matcher = null, Driver $driver = null, ServerFactory $factory = null) { $handler = new Internal\WebSocketRequestHandler($matcher ?: new DefaultProtocolMatcher(), $handler); $driver = $driver ?: new WebSocketDriver(); parent::__construct($handler, $log, $driver, $factory); }
/** * TODO: this implementation is not correct some error handling. */ function createServer(Closure $closure) { $server = new HttpServer(); $server->addListener($closure); return $server; }
#!/usr/bin/env php <?php define('zoop_dir', dirname(__FILE__) . '/../../framework'); define('app_dir', dirname(__FILE__)); include zoop_dir . '/Zoop.php'; Zoop::loadLib('Pherver'); $chatserver = new HttpServer(); $chatserver->run('127.0.0.1', 9050);
<?php require_once './http/server.php'; $server = new HttpServer(); $server->start();
public static function getInstance($webPath, $config = 'default') { if (!self::$instance) { self::$instance = new HttpServer($webPath, $config); } return self::$instance; }
public static function getItem($array, $key = '', $default = 0) { return isset($array[$key]) ? $array[$key] : $default; } //编码转换utf->gbk public static function changeCharset($value, $from = 'utf-8', $to = 'gbk//ignore') { $result = array(); if (is_array($value)) { foreach ($value as $k => $v) { $k = self::changeCharset($k, $from, $to); $result[$k] = self::changeCharset($v, $from, $to); } } else { $value = is_numeric($value) && floatval((int) $value) === floatval($value) ? (int) $value : $value; $result = is_string($value) ? iconv($from, $to, $value) : $value; } return $result; } } $AppSvr = new HttpServer(); $AppSvr->loadSetting(__DIR__ . '/swoole.ini'); //加载配置文件 $AppSvr->setDocumentRoot(__DIR__ . '/webroot'); $AppSvr->setLogger(new Swoole\Log\EchoLog(true)); //Logger Swoole\Error::$echo_html = false; $server = Swoole\Network\Server::autoCreate('0.0.0.0', 8888); $server->setProtocol($AppSvr); //$server->daemonize(); //作为守护进程 $server->run(array('worker_num' => 0, 'max_request' => 5000, 'log_file' => '/tmp/swoole.log'));
$line = $error['line']; $log = "{$message} ({$file}:{$line})\nStack trace:\n"; $trace = debug_backtrace(); foreach ($trace as $i => $t) { if (!isset($t['file'])) { $t['file'] = 'unknown'; } if (!isset($t['line'])) { $t['line'] = 0; } if (!isset($t['function'])) { $t['function'] = 'unknown'; } $log .= "#{$i} {$t['file']}({$t['line']}): "; if (isset($t['object']) && is_object($t['object'])) { $log .= get_class($t['object']) . '->'; } $log .= "{$t['function']}()\n"; } if (isset($_SERVER['REQUEST_URI'])) { $log .= '[QUERY] ' . $_SERVER['REQUEST_URI']; } error_log($log); $this->response($this->currentFd, $log); } } $server = new HttpServer(); $server->onRequest(function ($server) { $server->response("<h1>hello world</h1>"); }); $server->run();
<?php /** * @Author: WenJun * @Date: 15/10/23 15:37 * @Email: wenjun01@baidu.com * @File: server.php * @Desc: ... */ ini_set('memory_limit', '2048M'); error_reporting(E_ALL); date_default_timezone_set('Asia/Shanghai'); define('DS', DIRECTORY_SEPARATOR); define('ROOT_PATH', dirname(__FILE__)); define('CONF_PATH', ROOT_PATH . DS . 'conf'); define('APPLICATION_PATH', ROOT_PATH . DS . 'application'); define('LIBRARY_PATH', APPLICATION_PATH . DS . 'library'); require LIBRARY_PATH . DS . 'HttpServer.php'; $serverObj = HttpServer::getInstance(); $serverObj->setServerConfigIni(CONF_PATH . DS . 'server.ini'); $serverObj->setAppConfigIni(CONF_PATH . DS . 'application.ini'); $serverObj->start();
function __construct($ini_file) { parent::__construct($ini_file); }