源码地址
安装EASYSWOOLE
- git clone https://github.com/easy-swoole/demo.git
- git checkout 3.x-chat
- /usr/local/php7/bin/php /usr/bin/composer install
- /usr/local/php7/bin/php vender/bin/easyswoole install
安装时遇到提示是否覆盖 EasySwooleEvent.php、 dev.php 请选择否 (输入 n 回车)
启动项目
- /usr/local/php7/bin/php easyswoole start
遇到问题
问题描述:在启动时无法投递一个异步任务到task_worker池中,导致聊天室无法加载用户相关信息
官方文档:https://wiki.swoole.com/wiki/page/134.html
环境配置:
swoole version 4.2.13
php version 7.2.14
easy swoole 3.1.14
namespace EasySwoole\EasySwoole\Swoole\Task;
class TaskManager
{
public static function async($task,$finishCallback = null,$taskWorkerId = -1)
{
/* 测试代码
$task_id = ServerManager::getInstance()->getSwooleServer()->task($task,-1, function (\swoole_server $serv, $task_id, $data) {
echo "Task Callback: ";
var_dump($task_id, $data);
});
var_dump($task_id);*/
//修复:针对回调函数判断即可
$finishCallback = is_null($finishCallback) ? function (\swoole_server $serv, $task_id, $data){} : $finishCallback;
return ServerManager::getInstance()->getSwooleServer()->task($task,$taskWorkerId,$finishCallback);
}