thinkphp6swoole异步协程⼀、安装thinkphp swoole扩展
composer require topthink/think-swoole
⼆、设置swoole配置⽂件
<?php
use think\swoole\websocket\socketio\Handler;
use think\swoole\websocket\socketio\Parser;
return[
'server'=>[
'host'=>env('SWOOLE_HOST','127.0.0.1'),// 监听地址
'port'=>env('SWOOLE_PORT',80),// 监听端⼝
'mode'=>SWOOLE_PROCESS,// 运⾏模式默认为SWOOLE_PROCESS
'sock_type'=>SWOOLE_SOCK_TCP,// sock type 默认为SWOOLE_SOCK_TCP
'options'=>[
'pid_file'=>runtime_path().'swoole.pid',
'log_file'=>runtime_path().'swoole.log',
'daemonize'=>true,//保护模式
// Normally this value should be 1~4 times larger according to your cpu cores.
'reactor_num'=>swoole_cpu_num(),
'worker_num'=>swoole_cpu_num(),
'task_worker_num'=>swoole_cpu_num(),
'enable_static_handler'=>true,
'document_root'=>root_path('public'),
'package_max_length'=>40*1024*1024,
'buffer_output_size'=>20*1024*1024,
'socket_buffer_size'=>256*1024*1024,
'user'=>'nobody'
],
],
'websocket'=>[
'enable'=>true,
'handler'=> Handler::class,
'parser'=> Parser::class,
'ping_interval'=>25000,
'ping_timeout'=>60000,
'room'=>[
'type'=>'table',
'table'=>[
'room_rows'=>4096,
'room_size'=>2048,
'client_rows'=>8192,
'client_size'=>2048,
],
'redis'=>[
'host'=>'127.0.0.1',
'port'=>6379,
'max_active'=>5,
'max_wait_time'=>5,
],
],
'listen'=>[],
'subscribe'=>[],
],
'rpc'=>[
'server'=>[
'enable'=>false,
'port'=>9000,
'services'=>[
],
],
],
'client'=>[
],
],
'hot_update'=>[
'enable'=>true,
'name'=>['*.php'],
'include'=>[app_path()],
'exclude'=>[],
],
//连接池
'pool'=>[
'db'=>[
'enable'=>true,
'max_active'=>3,
'max_wait_time'=>5,
],
'cache'=>[
'enable'=>true,
'max_active'=>3,
'max_wait_time'=>5,
],
//⾃定义连接池
],
'coroutine'=>[
'enable'=>true,
'flags'=>SWOOLE_HOOK_ALL, ],
thinkphp3'tables'=>[],
//每个worker⾥需要预加载以共⽤的实例'concretes'=>[],
/
/重置器
'resetters'=>[],
//每次请求前需要清空的实例
'instances'=>[],
//每次请求前需要重新执⾏的服务
'services'=>[],
];
常驻内存时需要开启保护模式
三、设置listen
<?php
// 事件定义⽂件
return[
'bind'=>[
],
'listen'=>[
'AppInit'=>[],
'HttpRun'=>[],
'HttpEnd'=>[],
'LogLevel'=>[],
'LogWrite'=>[],
'swoole.task'=>[
\app\listener\ModelTask::class
]
,
'swoole.finish'=>[
\app\listener\ModelFinishTask::class ]
],
'subscribe'=>[
],
];
<?php
declare(strict_types =1);
namespace app\listener;
use Swoole\Coroutine as Co;
class ModelTask
{
/**
* 事件监听处理
*
* @return mixed
*/
public function handle($event)
{
vardump($event);
return;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。