下面由workerman使用教學專欄來介紹Laravel5.3和Workerman結合使用(非同步)的方法,希望對需要的朋友有幫助!
網路查閱資料有現成和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客戶端往服務端寫資料---服務端發送郵件
推薦:Workerman教學
#以上是Laravel5.3和Workerman結合使用(非同步)的詳細內容。更多資訊請關注PHP中文網其他相關文章!