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 ZPHP\ZPHP;

$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 ZPHP\ZPHP;

$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:\phpStudy\WWW\swoole\chat\apps\ctrl\main\main.php

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

 

 

 

 

转载于:https://www.cnblogs.com/zouke1220/p/8443939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值