zoukankan      html  css  js  c++  java
  • swoole在线聊天学习笔记

    <?php
    $http=new swoole_http_server('0.0.0.0',9501);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        print_r($request);
    });
    
    $http->start();

    http://192.168.10.31:9501/swoole/chat/chat/http_server.php

     

    <?php
    $http=new swoole_http_server('0.0.0.0',9501);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        $response->end("hello world");
    });
    
    $http->start();

    <?php
    $http=new swoole_http_server('0.0.0.0',9501);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        //$response->end("hello world");
        $response->status(404);
        $response->end('404 not found');
    });
    
    $http->start();

    <?php
    $http=new swoole_http_server('0.0.0.0',9501);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        //$response->end("hello world");
        //$response->status(404);
        //$response->end('404 not found');
        $pathinfo=$request->server['path_info'];
    $in=strrpos($pathinfo,'/');
        $filename=substr($pathinfo,$in);
    $filename
    =__DIR__.$pathinfo;
    if
    (is_file($filename)){ $content=file_get_contents($filename); $response->end($content); }else{ $response->status(404); $response->end('404 not found'); } }); $http->start();

    <?php
    $http=new swoole_http_server('0.0.0.0',9501);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        //$response->end("hello world");
        //$response->status(404);
        //$response->end('404 not found');
        $pathinfo=$request->server['path_info'];
        $in=strrpos($pathinfo,'/');
        $filename=substr($pathinfo,$in);
        $filename=__DIR__.$filename;
        if(is_file($filename)){
             $ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
            //动态请求处理
            if($ext=='php'){
                ob_start();
                include_once $filename;
                $content=ob_get_contents();
                ob_end_clean();
                $response->end($content);
            }else{ //静态请求处理
                $content=file_get_contents($filename);
                $response->end($content);
            }        
        }else{
            $response->status(404);
            $response->end('404 not found');
        }
    });
    
    $http->start();

    运行在nginx服务器

    运行在swoole--http_server服务器

    <?php
    $http=new swoole_http_server('0.0.0.0',9501);
    
    $http->set([
        'worker_num'=>16,
        'daemonize'=>1
    ]);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        //$response->end("hello world");
        //$response->status(404);
        //$response->end('404 not found');
        $pathinfo=$request->server['path_info'];
        $in=strrpos($pathinfo,'/');
        $filename=substr($pathinfo,$in);
        $filename=__DIR__.$filename;
        if(is_file($filename)){
            $ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
            //动态请求处理
            if($ext=='php'){
                ob_start();
                include_once $filename;
                $content=ob_get_contents();
                ob_end_clean();
                $response->end($content);
            }else{ //静态请求处理
                $content=file_get_contents($filename);
                $response->end($content);
            }        
        }else{
            $response->status(404);
            $response->end('404 not found');
        }
    });
    
    $http->start();

    设置完成后(daemonize=1时),http_server服务器转入后台作为守护进程运行

    <?php
    $http=new swoole_http_server('0.0.0.0',9501);
    
    $http->set([
        'worker_num'=>16,
        'daemonize'=>1
    ]);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        //$response->end("hello world");
        //$response->status(404);
        //$response->end('404 not found');
        $pathinfo=$request->server['path_info'];
        $in=strrpos($pathinfo,'/');
        $filename=substr($pathinfo,$in);
        $filename=__DIR__.$filename;
        if(is_file($filename)){
            $ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
            //动态请求处理
            if($ext=='php'){
                ob_start();
                include_once $filename;
                $content=ob_get_contents();
                ob_end_clean();
                $response->end($content);
            }else{ //静态请求处理
                $mimes=include('mimes.php');
                $response->header("Content-type",$mimes[$ext]);
                $content=file_get_contents($filename);
                $response->end($content);
            }        
        }else{
            $response->status(404);
            $response->end('404 not found');
        }
    });
    
    $http->start();

    ZPHP框架

    https://github.com/shenzhe/zphp

    执行脚手架生成项目

     将生成的项目文件全部复制到chat目录下  cp -rf chat_zphp/* chat//

    将zphp框架文件全部复制到chat目录下  cp -rf zphp/* chat//

    <?php
    use ZPHPPHP;
    
    $zphp=null;
    $mimes=null;
    
    $http=new swoole_http_server('0.0.0.0',9503);
    
    $http->set([
        'worker_num'=>16,
        'daemonize'=>0
    ]);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        //$response->end("hello world");
        //$response->status(404);
        //$response->end('404 not found');
        $pathinfo=$request->server['path_info'];
        $in=strrpos($pathinfo,'/');
        $filename=substr($pathinfo,$in);
        $filename=__DIR__.$filename;
        if(is_file($filename)){
            $ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
            //动态请求处理
            if($ext=='php'){
                /************************
                ob_start();
                include_once $filename;
                $content=ob_get_contents();
                ob_end_clean();
                $response->end($content);
                ************************/
                $response->status(404);
                $response->end('404 not found');
            }else{ //静态请求处理
                //$mimes=include('mimes.php');
                global $mimes;
                $response->header("Content-type",$mimes[$ext]);
                $content=file_get_contents($filename);
                $response->end($content);
            }        
        }else{
            //$response->status(404);
            //$response->end('404 not found');
            global $zphp;
            ob_start();
            $zphp->run();
            $result=ob_get_contents();
            ob_end_clean();
            $response->end($result);
        }
    });
    
    $http->on('workerStart',function($serv,$worker_id){
        //require zphp框架目录地址
        require __DIR__.DIRECTORY_SEPARATOR.'zphp'.DIRECTORY_SEPARATOR.'ZPHP'.DIRECTORY_SEPARATOR.'ZPHP.php';
        //home/wwwroot/default/swoole/www.zphp.com 是应用的地址
        $webPath=__DIR__;     //项目目录
        $configPath='default';//配置的目录的名称
        global $zphp;
        $zphp=ZPHP::run($webPath,false,$configPath);
        global $mimes;
        $mimes=require 'mimes.php';
    });
    $http->start();
    
    /************************************************************************
    master           //连接的处理(收发管理)  多线程(reactor,heartbeat)  epoll
       manager       //fork 管理worker进程
           worker    //worker进程 业务逻辑处理 
    ************************************************************************/

     代码热更新

    <?php
    use ZPHPPHP;
    
    $zphp=null;
    $mimes=null;
    
    $http=new swoole_http_server('0.0.0.0',9503);
    
    $http->set([
        'worker_num'=>16,
        'daemonize'=>0
    ]);
    
    $http->on('request',function(swoole_http_request $request,swoole_http_response $response){
        //print_r($request);
        //$response->end("hello world");
        //$response->status(404);
        //$response->end('404 not found');
        $pathinfo=$request->server['path_info'];
        $in=strrpos($pathinfo,'/');
        $filename=substr($pathinfo,$in);
        $filename=__DIR__.$filename;
        if(is_file($filename)){
            $ext=pathinfo($request->server['path_info'],PATHINFO_EXTENSION);
            //动态请求处理
            if($ext=='php'){
                /************************
                ob_start();
                include_once $filename;
                $content=ob_get_contents();
                ob_end_clean();
                $response->end($content);
                ************************/
                $response->status(404);
                $response->end('404 not found');
            }else{ //静态请求处理
                //$mimes=include('mimes.php');
                global $mimes;
                $response->header("Content-type",$mimes[$ext]);
                $content=file_get_contents($filename);
                $response->end($content);
            }        
        }else{
            //$response->status(404);
            //$response->end('404 not found');
            $_GET=$_POST=$_COOKIE=$_REQUEST=[];
            if(!empty($request->get)){
                $_GET=$request->get;
                $_REQUEST +=$_GET;
            }
            if(!empty($request->post)){
                $_POST=$request->post;
                $_REQUEST +=$_POST;
            }
            if(!empty($request->cookie)){
                $_COOKIE=$request->cookie;
            }
            global $zphp;
            ob_start();
            $zphp->run();
            $result=ob_get_contents();
            ob_end_clean();
            $response->end($result);
        }
    });
    
    $http->on('workerStart',function($serv,$worker_id){
        //require zphp框架目录地址
        require __DIR__.DIRECTORY_SEPARATOR.'zphp'.DIRECTORY_SEPARATOR.'ZPHP'.DIRECTORY_SEPARATOR.'ZPHP.php';
        //home/wwwroot/default/swoole/www.zphp.com 是应用的地址
        $webPath=__DIR__;     //项目目录
        $configPath='default';//配置的目录的名称
        global $zphp;
        $zphp=ZPHP::run($webPath,false,$configPath);
        global $mimes;
        $mimes=require 'mimes.php';
    });
    $http->start();
    
    /************************************************************************
    master           //连接的处理(收发管理)  多线程(reactor,heartbeat)  epoll
       manager       //fork 管理worker进程
           worker    //worker进程 业务逻辑处理 
    ************************************************************************/

    D:phpStudyWWWswoolechatappsctrlmainmain.php

     public function main()
        {
            $project = Config::getField('project', 'name', 'zphp');
            $data = $project." runing in swoole!
    ";
            $params = $_REQUEST;//Request::getParams();
            if(!empty($params)) {
                foreach($params as $key=>$val) {
                    $data.= "key:{$key}=>{$val}
    ";
                }
            }
            print_r($data);
            return $data;
        }

     

     

  • 相关阅读:
    MySQL索引底层的实现
    mysql索引深入优化
    explain详解与索引最佳实践
    (MYSQL)回表查询原理,利用联合索引实现索引覆盖
    为什么重复值高的字段不能建索引(比如性别字段等)
    Spring的事务
    为什么要用Redis?Redis为什么这么快?
    spring AOP
    钩子教程
    钩子教程
  • 原文地址:https://www.cnblogs.com/zouke1220/p/8443939.html
Copyright © 2011-2022 走看看