本想本身适配的,奈何keng貌似很多,因此果断选择官方提供的包来适配233。。。php
默认条件:thinkphp5.1.*版本下,且安装了swoole扩展git
主要演示:task 任务的投递github
友情提示:在swoole启动框架时,需注意静态变量的使用,会常驻内存(好比单例的变量),... 无图言卵,我么的口号是有图有真相(搞事)
web
如下是swoole下单例模式发送邮件:thinkphp
请看收件人, 图一给 994xxx@qq.com发送邮件; 图二给 159xxx@qq.com 发送邮件,其收件人却有两(包含图一中的帐号)。json
composer require topthink/think-swoole=2.0.*
若是你要运行 swoole 的 http 服务器来启动 thinkPHP框架,执行服务器
php think swoole
默认启动完成后,会在0.0.0.0:9501启动一个HTTP Server,能够直接访问当前的应用。websocket
swoole的参数能够在应用配置目录下的 swoole.php 里面配置,配置详情见 thinkPHP官网swoole
扩展中定义了 onWorkerStart
和 onRequest
事件回调方法(若是不熟悉请不要随意替换),若是你须要自定义swoole
的事件回调方法,能够在配置文件中使用闭包定义。闭包
如下是个人配置(自定义了task任务回调):
use app\common\lib\utils\Task; use think\facade\Env; use think\facade\Log; // +---------------------------------------------------------------------- // | Swoole设置 php think swoole命令行下有效 // +---------------------------------------------------------------------- return [ // 扩展自身配置 'host' => '0.0.0.0', // 监听地址 'port' => 9501, // 监听端口 'mode' => '', // 运行模式 默认为SWOOLE_PROCESS 'sock_type' => '', // sock type 默认为SWOOLE_SOCK_TCP 'server_type' => 'http', // 服务类型 支持 http websocket 'app_path' => '', // 应用地址 若是开启了 'daemonize'=>true 必须设置(使用绝对路径) 'file_monitor' => false, // 是否开启PHP文件更改监控(调试模式下自动开启) 'file_monitor_interval' => 2, // 文件变化监控检测时间间隔(秒) 'file_monitor_path' => [], // 文件监控目录 默认监控application和config目录 // 能够支持swoole的全部配置参数 'pid_file' => Env::get('runtime_path') . 'swoole.pid', 'log_file' => Env::get('runtime_path') . 'swoole.log', 'document_root' => Env::get('root_path') . 'public', 'enable_static_handler' => true, 'timer' => true,//是否开启系统定时器 'interval' => 500,//系统定时器 时间间隔 'task_worker_num' => 1,//swoole 任务工做进程数量 /** * 自定义投递任务 * @param swoole_server $serv * @param int $taskId * @param int $srcWorkerId * @param mixed $data */ 'Task' => function($serv, $taskId, $srcWorkerId, $data){ $taskObj = new Task(); $classMethods = get_class_methods(Task::class); if (!in_array($data['method'], $classMethods)) { return 'method:'.$data['method'].' not find in'.Task::class; } return call_user_func_array([$taskObj, $data['method']], $data['params']); }, /** * onTask事件中没有调用finish方法或者return结果,worker进程不会触发onFinish * @Param swoole_server $serv * @param int $taskId 任务的ID * @param string $data 任务处理的结果内容 */ 'Finish' => function ($serv, $taskId, $data) { // echo 'taskId:' . $taskId . PHP_EOL; echo 'Finished:' . $data; Log::record($data); } ];
Index控制器中:
<?php namespace app\index\controller; use app\common\lib\task\SmsTask; use app\common\lib\utils\Tool;use think\Controller; use think\Request; class Index extends Controller {
public function sendSms(Request $request) { $result = $this->validate($request->post(), ['mobile' => 'require|mobile']); if (true !== $result) { return Tool::json('', $result, 250); } $mobile = $request->post('mobile'); // 一、使用topthink/swoole自带的任务投递方式,传递参数必须是对象或者swoole回调函数 // $smsObj = new SmsTask($mobile); // app('swoole')->task($smsObj); // app('swoole')->task($smsObj); // 二、自定义任务投递方式 app('swoole')->task(Tool::taskParam('sendSms', (array)$mobile)); return Tool::json('', '短信发送成功'); }
SmsTask.php
使用系统默认的回调模板,
且投递的参数必须是对象或者swoole回调函数,
投递任务后默认会执行 run()方法或者swoole回调函数
<?php namespace app\common\lib\task; use Fairy\SmsSender; use think\facade\Config; use think\swoole\template\Task; /** * 使用 topthink/swoole 自带的Task任务 * Class SmsTask * @package app\common\lib\task */ class SmsTask extends Task { private $mobile; public function initialize($args) { // TODO: Implement initialize() method. $this->mobile = $args[0]; } public function run($serv, $taskId, $fromWorkerId) { // TODO: Implement run() method. $smsObj = SmsSender::getInstance(Config::get('mail.')); $bool = $smsObj->send($this->mobile); if ($bool) { return 'send sms to ' . $this->mobile . ' success'; } else { return $smsObj->getError(); } } }
Task.php
配合swoole.php中自定义的 task 回调函数使用
<?php namespace app\common\lib\utils; use Fairy\SmsSender;use think\facade\Config; /** * 异步任务类 * Class Task * @package app\common\lib\utils */ class Task { /** * 异步发送短信 * @param $mobile * @return mixed|string * @throws \ErrorException */ public function sendSms($mobile) { $smsObj = SmsSender::getInstance(Config::get('sms.')); $bool = $smsObj->send($mobile); if ($bool) { return 'send sms to ' . $mobile . ' success'; } else { return 'send sms to ' . $mobile . ' failed: ' . $smsObj->getError(); } } }
参考:
https://www.kancloud.cn/manual/thinkphp5_1/675277