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

  • 相关阅读:
    sort排序
    js数组
    json数据格式 与 for in
    js 定时器
    鼠标滚轮事件
    cookie
    POJ 2387 Til the Cows Come Home
    POJ 1459 Power Network
    HDU 5389 Zero Escape
    HDU 5387 Clock
  • 原文地址:https://www.cnblogs.com/brady-wang/p/11517736.html
Copyright © 2011-2022 走看看