聊聊Laravel中怎么接入workerman

laravel+websocket是即时通讯开发必备利器,那么Laravel中怎么接入workerman?下面本篇文章给大家介绍一下laravel快速接入websocket的方法,希望对大家有所帮助。

聊聊Laravel中怎么接入workerman

前言:此处,我们采用基于workerman开发的GatewayWorker作为socket的核心组件,因为,workerman是一个纯粹的socket类库,相对于GatewayWorker来说,更底层,更需要开发者有多进程的编程经验,而我们大部分开发者使用workerman是为了做IM、或者游戏等,直接基于GatewayWorker开发,更快、更强。

一、安装gateway-worker

composer require workerman/gateway-worker -vvv

二、创建Workerman启动文件

通过下列命令,在App\Console\Commands下创建命令行文件,并将下方代码复制到文件中。【相关推荐:《workerman教程》】

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 = &#39;workman {action} {--d}&#39;; //执行该命令的方式
 
    protected $description = &#39;Start a Workerman server.&#39;;
 
    public function handle()
    {
        global $argv;
        $action = $this->argument(&#39;action&#39;);
 
        $argv[0] = &#39;wk&#39;;
        $argv[1] = $action;
        $argv[2] = $this->option(&#39;d&#39;) ? &#39;-d&#39; : &#39;&#39;; 
      // -d守护模式,不会因为关闭系统命令页面而被杀掉进程。 没有-d则关闭命令页面直接退出进程
 
        $this->start();
    }
 
    private function start()
    {
        $this->startGateWay();
        $this->startBusinessWorker();
        $this->startRegister();
        Worker::runAll();
    }
 
    private function startBusinessWorker()
    {
        $worker                  = new BusinessWorker();
        $worker->name            = &#39;BusinessWorker&#39;;
        $worker->count           = 1;
        $worker->registerAddress = &#39;127.0.0.1:1236&#39;;
        $worker->eventHandler    = \App\Events::class; //用作监听事件的文件
    }
 
    private function startGateWay()
    {
//因为小程序等一些平台,要求使用wss进行socket,所以,这里需要配置下wss
//此处的cert.pem和key.key是域名的证书文件
       $content = array(
            &#39;ssl&#39; => array(
                &#39;local_cert&#39; => public_path(&#39;cert.pem&#39;),
                &#39;local_pk&#39; => public_path(&#39;key.key&#39;),
                &#39;verify_peer&#39; => false
            )
        );
        $gateway = new Gateway("websocket://0.0.0.0:2346", $content);
        //如果不需要wss,则不用加入content这个参数
        $gateway->transport = &#39;ssl&#39;;//不需要wss,也不用加入这个参数。
        $gateway->name                 = &#39;Gateway&#39;;
        $gateway->count                = 1;
        $gateway->lanIp                = &#39;127.0.0.1&#39;;
        $gateway->startPort            = 2300;
        $gateway->pingInterval         = 30;
        $gateway->pingNotResponseLimit = 0;
         $data = array(
            &#39;type&#39; => &#39;heart&#39;
        );
        $gateway->pingData = json_encode($data, true);
        $gateway->registerAddress      = &#39;127.0.0.1:1236&#39;;
    }
 
    private function startRegister()
    {
        new Register(&#39;text://0.0.0.0:1236&#39;);
    }
}

三、创建监听事件

创建一个app/Events.php文件来监听处理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

在命令行里面执行,支持的命令有 start | stop | restart,后续加 -d 的意思是守护模式【daemon】

php artisan workman start -d

五、踩坑关键点

1、在LINUX环境中使用。

2、有可能会启动失败,此时,请检查php中,是否禁用了pcntl开头的相关方法。在 php配置文件中查找到disable_functions,将所有pcntl开头的方法全部删除。

六、GatewayWorker手册

gateway-worker手册

更多编程相关知识,请访问:编程教学!!

以上就是聊聊Laravel中怎么接入workerman的详细内容,更多请关注https://www.sxiaw.com/其它相关文章!