PHP code example of chenjiabin / think5.0-swoole

1. Go to this page and download the library: Download chenjiabin/think5.0-swoole library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chenjiabin / think5.0-swoole example snippets




namespace app\index\controller;

use xavier\swoole\Task;


class Index
{
    public function index()
    {
        return "index";
    }

    public function test()
    {
        echo 1;
        $param = request()->param();
        $post  = request()->post();

        //异步任务投递
        Task::async(function ($serv, $task_id, $data) {
            $i = 0;
            while ($i < 10) {
                $i++;
                echo $i;
                sleep(1);
            }
        });
        //使用swoole4.0协程
        \go(function () {
            $i = 0;
            while ($i < 10) {
                echo 2;
                $i++;
                \co::sleep(1);
            };
        });
        return json($post);
    }
}



return [
    'host'                  => '0.0.0.0', // 监听地址
    'port'                  => 9501, // 监听端口
    'mode'                  => '', // 运行模式 默认为SWOOLE_PROCESS
    'sock_type'             => '', // sock type 默认为SWOOLE_SOCK_TCP
    'app_path'              => getcwd() . '/application', // 应用地址 如果开启了 'daemonize'=>true 必须设置(使用绝对路径)
    'file_monitor'          => false, // 是否开启PHP文件更改监控(调试模式下自动开启)
    'file_monitor_interval' => 2, // 文件变化监控检测时间间隔(秒)
    'file_monitor_path'     => [], // 文件监控目录 默认监控application和config目录
    // 可以支持swoole的所有配置参数
    'pid_file'              => getcwd()  . '/runtime/swoole.pid',
    'log_file'              => getcwd()  . '/runtime/swoole.log',
    'task_worker_num'       => 20,
    //'document_root'         => getcwd() . 'public',
    //'enable_static_handler' => true,
    'daemonize'                => 1,//守护
    'worker_num' => 8,    //worker process num
    'max_request' => 10000,
];

public static function destroy()
    {
        if (!is_null(self::$instance)) {
            self::$instance=null;
        }
    }

/**
     * 当前的请求类型
     * @access public
     * @param bool $method true 获取原始请求类型
     * @return string
     */
    public function method($method = false)
        {
            if (true === $method) {
                // 获取原始请求类型
                $this->method = IS_CLI ?(defined('IS_SWOOLE')?((isset($this->server['REQUEST_METHOD'])? $this->server['REQUEST_METHOD'] : isset($_SERVER['REQUEST_METHOD'])?$_SERVER['REQUEST_METHOD']:'GET')):'GET') : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
            } elseif (!$this->method) {
                if (isset($_POST[Config::get('var_method')])) {
                    $this->method = strtoupper($_POST[Config::get('var_method')]);
                    $this->{$this->method}($_POST);
                } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
                    $this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
                } else {
                    $this->method = IS_CLI ?(defined('IS_SWOOLE')?((isset($this->server['REQUEST_METHOD'])? $this->server['REQUEST_METHOD'] : isset($_SERVER['REQUEST_METHOD'])?$_SERVER['REQUEST_METHOD']:'GET')):'GET') : (isset($this->server['REQUEST_METHOD']) ? $this->server['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']);
                }
            }
    
            return $this->method;
        }



namespace app\index\controller;

use xavier\swoole\Task;
use xavier\swoole\Timer;

use xavier\swoole\Component\HttpClient;

class Index
{
    public function __construct()
    {

    }

    public function index()
    {
        return "indexss";
    }

    public function test()
    {
        $param = request()->param();
        $post  = request()->post();


        Task::async(function ($serv, $task_id, $data)use($post) {
            $i = 0;
            while ($i < 10) {
                $i++;
                echo $i;
                //var_dump($post);
                sleep(1);
            }
        });

        //Task::async("\\app\\lib\\Task");
        $task=new \app\lib\Task($post,1);
        Task::async($task);
        \go(function ()use($post) {
            $i = 0;
            while ($i < 10) {
                $i++;
                //var_dump($post);
                \co::sleep(1);
            };
        });
        return json($post);
    }

    public function send()
    {
        $data  = [1, 2, 3,];
        $datas = ['a', 'b1232'];
        $redt  = HttpClient::instance("http://127.0.0.1:9501")->url('index/index')->gets([
            'data'  => $data,
            'datas' => $datas,
        ]);
        var_dump($redt);
    }

    public function sendhtml()
    {
        $data  = [1, 2, 3,];
        $datas = ['a', 'b1232'];
        $redt  = HttpClient::instance("http://127.0.0.1:9501")->url('index/index')->setContentType('html')->test([
            'data'  => $data,
            'datas' => $datas,
        ]);
        var_dump($redt);
    }

    public function gets($data=null,$datas=null)
    {
        return json(array_merge($data,$datas));

    }


}




/**
 * Created by PhpStorm.
 * User: xavier
 * Date: 2018/8/15
 * Time: 下午5:45
 */

namespace app\lib;

use xavier\swoole\template\Timer as TimerC;
class Timer extends TimerC
{
    public function _initialize(...$arg)
    {
        // TODO: Implement _initialize() method.
    }

    public function run()
    {
        // TODO: Implement run() method.
        var_dump('timer');
    }
}


/**
 * Created by PhpStorm.
 * User: xavier
 * Date: 2018/8/15
 * Time: 下午2:14
 * 秒 分 时 日 月 星期几
 * crontab 格式 * *  *  *  * *    => "类"
 * *中间一个空格
 * 系统定时任务需要在swoole.php中开启
 * 自定义定时器不受其影响
 */

return [
    '*/5 * * * * *'=>'\\app\\lib\\Timer',//每5秒执行一次,从第一位一次表示 秒,分,时,日,月
];


use xavier\swoole\Timer;

//支持回调
Timer::tick(1000,function(){
    
});

//支持执行定时器接口实现的类
Timer::tick(1000,'\\app\\lib\\Timer');



namespace app\lib;
/**
 * Created by PhpStorm.
 * User: xavier
 * Date: 2018/8/24
 * Time: 上午9:26
 * Email:[email protected]
 */
use xavier\swoole\template\WorkerStart as Worker;
class WorkerStart extends Worker
{
    public function _initialize($server, $worker_id)
    {
        // TODO: Implement _initialize() method.
    }

    public function run()
    {
        
    }
}

'wokerstart'=>'\\app\\lib\\WorkStart'

'wokerstart'=>function($server, $worker_id){
    //如果只在一个进程处理 则可以这样

    if (0==$woker_id){
        //这样只会在第一个woker进程处理
    }
}

'cache'                  => [
        // 驱动方式
        'type'   => 'Table',
        // 缓存前缀
        'prefix' => '',
        // 缓存有效期 0表示永久缓存
        'expire' => 0,
    ],
sh
php think swoole start
sh
php think swoole start -d
sh
php think swoole stop