4

前言

ThinkPHP即将迎来最新版本6.0,针对目前越来越流行Swoole,thinkphp也推出了最新的扩展think-swoole 3.0

安装

由于目前thinkphp 6.0没有稳定版本,所以只能安装开发板

composer create-project topthink/think tp 6.0.*-dev

接下来安装think-swoole 3.0,目前最新的稳定版本是3.0.2

composer require topthink/think-swoole

配置

安装结束可以根据自己的需求对配置信息进行修改。TP6的配置信息都存在于外部的config目录,这里主要介绍swoole相关配置信息

use think\swoole\websocket\room\TableRoom;
use think\swoole\websocket\socketio\Handler;
use think\swoole\websocket\socketio\Parser;

return [
    'server'           => [
        'host'      => '0.0.0.0', // 监听地址
        'port'      => 80, // 监听端口
        'mode'      => SWOOLE_PROCESS, // 运行模式 默认为SWOOLE_PROCESS
        'sock_type' => SWOOLE_SOCK_TCP, // sock type 默认为SWOOLE_SOCK_TCP
        'options'   => [
            'pid_file'              => runtime_path() . 'swoole.pid',//主进程ID保存文件路径
            'log_file'              => runtime_path() . 'swoole.log',//swoole日志文件
            'daemonize'             => false,
            // Normally this value should be 1~4 times larger according to your cpu cores.
            'reactor_num'           => swoole_cpu_num(),//线程数,默认值即可,不设置也可以
            'worker_num'            => swoole_cpu_num(),//worker进程数量
            'task_worker_num'       => swoole_cpu_num(),//异步任务进程数量
            'enable_static_handler' => true,//是否启用静态服务,如果开启,则优先判断指定的web目录下是否存在请求的静态文件,如果存在,则直接返回
            'document_root'         => root_path('public'),//web目录
            'package_max_length'    => 20 * 1024 * 1024,
            'buffer_output_size'    => 10 * 1024 * 1024,
            'socket_buffer_size'    => 128 * 1024 * 1024,
            'max_request'           => 3000,
            'send_yield'            => true,
        ],
    ],
    'websocket'        => [
        'enabled'       => false,//是否开启
        'handler'       => Handler::class,//处理请求的类,可以自定义
        'parser'        => Parser::class,//处理解析的类,可以自定义
        'route_file'    => base_path() . 'websocket.php',//websocket路由文件
        'ping_interval' => 25000,
        'ping_timeout'  => 60000,
        'room'          => [
            'type'        => TableRoom::class,
            'room_rows'   => 4096,
            'room_size'   => 2048,
            'client_rows' => 8192,
            'client_size' => 2048,
        ],
    ],
    'auto_reload'      => false,
    'enable_coroutine' => true,
    'resetters'        => [],
    'tables'           => [],
];

启动

php think swoole

执行上述命令则可以启动web服务

如果需要使用守护进程方式运行,可以配置

'options'   =>  [
    'daemonize' =>  true
]

支持的命令

php think swoole [start|stop|reload|restart]

aa杨
1.7k 声望44 粉丝