zoukankan      html  css  js  c++  java
  • swoole httpserver学习

    文件 HttpServer.php

    <?php
    /**
     * Created by PhpStorm.
     * User: mac
     * Date: 2019/9/13
     * Time: 21:00
     */
    
    class HttpServer
    {
    	public $http_serv;
    
    	const PORT = 9501;
    	const IP = "0.0.0.0";
    
    	public $static_handel_switch = true;
    	public $document_root = '/www/swoole';
    
    	public function __construct()
    	{
    		$this->http_serv = new SwooleHttpServer(self::IP,self::PORT);
    
    		$this->http_serv->on("request",[$this,"onRequest"]);
    
    		if($this->static_handel_switch == true){
    			$this->enableStaticHandel($this->document_root);
    		}
    	}
    
    
    	/**
    	 * 接受到客户端请求
    	 * @param $request 请求
    	 * @param $response 响应
    	 */
    	public function onRequest($request,$response)
    	{
    
    
    		//var_dump($request->server['request_uri']);
    		if($request->server['request_uri'] != "/favicon.ico"){
    
    
    			$headers = [
    				'Content-Type'=>"text/html; charset=utf-8"
    			];
    
    			$this->setHeader($headers,$response);
    
    			$response->end("<h1>hello swoole ".rand(100,999)."</h1>");
    		}
    
    
    	}
    
    	public function setHeader(array $headers ,$response)
    	{
    		foreach($headers as $key=>$header){
    			$response->header($key,$header);
    		}
    	}
    
    	/**
    	 * 启动
    	 */
    	public function start()
    	{
    		$this->http_serv->start();
    	}
    
    	public function enableStaticHandel($document_root)
    	{
    		$this->http_serv->set(
    			[
    				'document_root' =>  $document_root, // v4.4.0以下版本, 此处必须为绝对路径
    				'enable_static_handler' => true,
    			]
    		);
    	}
    }
    
    $http = new HttpServer();
    $http->start();
    

    cli执行

    php HttpServer.php

    浏览器访问

    http://192.168.1.200:9501   ip为自己虚拟机ip

  • 相关阅读:
    176. Second Highest Salary
    175. Combine Two Tables
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
    169. Majority Element
    168. Excel Sheet Column Title
    167. Two Sum II
    160. Intersection of Two Linked Lists
    个人博客记录
    <meta>标签
  • 原文地址:https://www.cnblogs.com/brady-wang/p/11517736.html
Copyright © 2011-2022 走看看