网上查阅资料有现成和workerman结合的composer组件,但个人感觉不太靠谱,github上star太少,而且怕有问题也不好调,就想自己先试试。
我的办法因为修改要一点Workerman源码,且是直接引入Workerman的代码文件,感觉有点low,但才智有限,暂时没想到更好的方法。
准备:
1.需先了解Laravel框架下命令行的使用,参考中文版教程
2.需了解Workerman的基本知识
场景:用户注册后,异步方式给用户发送邮件提醒
1.把Workerman框架放到app目录下
2.创建命令代码:
php artisan make:command SendEmail
namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Illuminate\Mail\Message; use Workerman\Worker; require app_path('Workerman/Workerman_Linux/Autoloader.php'); class SendEmail extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'send:email {action}'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * */ public function handle() { $mailWorker = new Worker('Text://0.0.0.0:12345'); $mailWorker->count = 4; $mailWorker->name = 'MailWorker'; $mailWorker->onMessage = function ($connection, $emailData) { $emailData = json_decode($emailData); $name = $emailData->name; $email = $emailData->to; Mail::raw('注册成功', function (Message $message) use ($email) { $message->to($email)->subject(trans('mail.welcome_register')); }); // 写入日志 Log::useFiles(storage_path() . '/logs/event.log', 'info'); Log::info("{$name}({$email})注册成功"); }; Worker::runAll(); } }
以上是workerman服务端,用命令行启动:
php artisan send:email start
此时命令行下会报错:Workerman[artisan] not run,原因是Workerman会把第一个参数artisan当前启动文件,而实际上,send:email才是我们想要的启动文件
解决:修改Workerman的解析参数代码
Workerman\Workerman_Linux\Worker.php,修改parseCommand方法(只要把$argv的键都加1就行):
/** * Parse command. * php yourfile.php start | stop | restart | reload | status * * @return void */ protected static function parseCommand() { global $argv; if($argv[0] == 'artisan') // laravel框架下处理 { // Check argv; $start_file = $argv[1]; if (!isset($argv[2])) { exit("Usage: php yourfile.php {start|stop|restart|reload|status}\n"); } // Get command. $command = trim($argv[2]); $command2 = isset($argv[3]) ? $argv[3] : ''; } else { // Check argv; $start_file = $argv[0]; if (!isset($argv[1])) { exit("Usage: php yourfile.php {start|stop|restart|reload|status}\n"); } // Get command. $command = trim($argv[1]); $command2 = isset($argv[2]) ? $argv[2] : ''; } // 只要略修改上面的参数解析部分即可 .......................... }
重新启动OK:
php artisan send:email start
3.服务端完成了,下面是客户端代码
我的邮件操作代码是当作事件逻辑,所以在事件监听器文件写代码:
app\Listeners\SendMailEventListener.php:
<?php namespace App\Listeners; use App\Events\SendMailEvent;class SendMailEventListener extends BaseEventListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Handle the event. * * @param SendMailEvent $event * @return void */ public function handle($event) { // 发送邮件通知注册成功 if ($event->user->scene == 'do_register') { $email = $event->user->email; //$ip = "mail_worker 的ip" ,本机的话为127.0.0.1 $socket = @stream_socket_client('tcp://127.0.0.1:12345', $errno, $errmsg, 5); if ($socket) { $mail_data = ['name'=>$event->user->name,'to' => $email, 'content' => trans('mail.welcome_register')]; // 注意,Text协议后面"\n"换行符是必须的 $mail_buffer = json_encode($mail_data) . "\n"; // 发送给mail worker fwrite($socket, $mail_buffer); } // $email = $event->user->email; // Mail::raw('注册成功',function (Message $message) use ($email) { // $message->to($email)->subject(trans('mail.welcome_register')); // }); } } }
4.总结步骤
启动服务端---注册用户---触发SendEmail事件---socket客户端往服务端写数据---服务端发送邮件
如果各位有更好的结合方式,一起交流下哈。