溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Laravel中如何接入workerman

發(fā)布時間:2022-12-08 09:10:38 來源:億速云 閱讀:132 作者:iii 欄目:編程語言

這篇文章主要介紹“Laravel中如何接入workerman”,在日常操作中,相信很多人在Laravel中如何接入workerman問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Laravel中如何接入workerman”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

一、安裝gateway-worker

composer require workerman/gateway-worker -vvv

二、創(chuàng)建Workerman啟動文件

通過下列命令,在App\Console\Commands下創(chuàng)建命令行文件,并將下方代碼復(fù)制到文件中。

php artisan make:command WorkermanCommand
<?php
 
namespace App\Console\Commands;
 
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Illuminate\Console\Command;
use Workerman\Worker;
 
class WorkermanCommand extends Command
{
 
    protected $signature = 'workman {action} {--d}'; //執(zhí)行該命令的方式
 
    protected $description = 'Start a Workerman server.';
 
    public function handle()
    {
        global $argv;
        $action = $this->argument('action');
 
        $argv[0] = 'wk';
        $argv[1] = $action;
        $argv[2] = $this->option('d') ? '-d' : ''; 
      // -d守護模式,不會因為關(guān)閉系統(tǒng)命令頁面而被殺掉進程。 沒有-d則關(guān)閉命令頁面直接退出進程
 
        $this->start();
    }
 
    private function start()
    {
        $this->startGateWay();
        $this->startBusinessWorker();
        $this->startRegister();
        Worker::runAll();
    }
 
    private function startBusinessWorker()
    {
        $worker                  = new BusinessWorker();
        $worker->name            = 'BusinessWorker';
        $worker->count           = 1;
        $worker->registerAddress = '127.0.0.1:1236';
        $worker->eventHandler    = \App\Events::class; //用作監(jiān)聽事件的文件
    }
 
    private function startGateWay()
    {
//因為小程序等一些平臺,要求使用wss進行socket,所以,這里需要配置下wss
//此處的cert.pem和key.key是域名的證書文件
       $content = array(
            'ssl' => array(
                'local_cert' => public_path('cert.pem'),
                'local_pk' => public_path('key.key'),
                'verify_peer' => false
            )
        );
        $gateway = new Gateway("websocket://0.0.0.0:2346", $content);
        //如果不需要wss,則不用加入content這個參數(shù)
        $gateway->transport = 'ssl';//不需要wss,也不用加入這個參數(shù)。
        $gateway->name                 = 'Gateway';
        $gateway->count                = 1;
        $gateway->lanIp                = '127.0.0.1';
        $gateway->startPort            = 2300;
        $gateway->pingInterval         = 30;
        $gateway->pingNotResponseLimit = 0;
         $data = array(
            'type' => 'heart'
        );
        $gateway->pingData = json_encode($data, true);
        $gateway->registerAddress      = '127.0.0.1:1236';
    }
 
    private function startRegister()
    {
        new Register('text://0.0.0.0:1236');
    }
}

三、創(chuàng)建監(jiān)聽事件

創(chuàng)建一個app/Events.php文件來監(jiān)聽處理Workman的各種事件

<?php
 
namespace App\Workerman;
 
class Events
{
 
    public static function onWorkerStart($businessWorker)
    {
    }
 
    public static function onConnect($client_id)
    {
    }
 
    public static function onWebSocketConnect($client_id, $data)
    {
    }
 
    public static function onMessage($client_id, $message)
    {
    }
 
    public static function onClose($client_id)
    {
    }
}

四、啟動workerman

在命令行里面執(zhí)行,支持的命令有 start | stop | restart,后續(xù)加 -d 的意思是守護模式【daemon】

php artisan workman start -d

五、踩坑關(guān)鍵點

1、在LINUX環(huán)境中使用。

2、有可能會啟動失敗,此時,請檢查php中,是否禁用了pcntl開頭的相關(guān)方法。在 php配置文件中查找到disable_functions,將所有pcntl開頭的方法全部刪除。

到此,關(guān)于“Laravel中如何接入workerman”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI