laravel 安装workerman及简单运用

醉菜鸟 2021-12-21 09:00:12

安装 gateway-worker

命令:composer require workerman/gateway-worker

创建WorkerMan 启动文件

命令: php artisan make:command WorkerMan
<?php

namespace App\Console\Commands;

use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Workerman\Worker;
use Illuminate\Console\Command;
use App\WorkerMan\Events;

class WorkerMan extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workman {action} {--d}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'start a WorkerMan server.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        global $argv;
        $action = $this->argument('action');
        $argv[0] = 'wk';
        $argv[1] = $action;
        $argv[2] = $this->option('d') ? '-d' : '';
        $this->start();
    }

    private function start()
    {
        $this->startGateway();
        $this->startBusinessWorker();
        $this->startRegister();
        Worker::runAll();
    }

    private function startBusinessWorker()
    {
        $worker                  = new BusinessWorker();
        //work名称
        $worker->name            = 'BusinessWorker';
        //businessWork进程数
        $worker->count           = 2;
        //服务注册地址
        $worker->registerAddress = '127.0.0.1:1236';
        //设置\App\Workerman\Events类来处理业务
        $worker->eventHandler    = Events::class;
    }

    private function startGateway()
    {
        //gateway进程
        $gateway = new Gateway("websocket://0.0.0.0:2346");
        //gateway名称 status方便查看
        $gateway->name                 = 'Gateway';
        //gateway进程
        $gateway->count                = 2;
        //本机ip
        $gateway->lanIp                = '127.0.0.1';
        //内部通讯起始端口,如果$gateway->count = 4 起始端口为2300
        //则一般会使用 2300,2301 2个端口作为内部通讯端口
        $gateway->startPort            = 2300;
        //心跳间隔
        $gateway->pingInterval         = 30;
        //客户端连续$pingNotResponseLimit次$pingInterval时间内不发送任何数据则断开链接,并触发onClose。
        //我们这里使用的是服务端主动发送心跳所以设置为0
        $gateway->pingNotResponseLimit = 0;
        //心跳数据
        $gateway->pingData             = '{"type":"@heart@"}';
        //服务注册地址
        $gateway->registerAddress      = '127.0.0.1:1236';
    }

    private function startRegister()
    {
        new Register('text://0.0.0.0:1236');
    }
}

创建监听文件 app/WorkerMan/Events.php

<?php

namespace App\WorkerMan;

use GatewayWorker\Lib\Gateway;

class Events
{
    // businessWorker进程启动事件
    public static function onWorkerStart($businessWorker)
    {

    }

    //连接事件
    public static function onConnect($client_id)
    {
        Gateway::sendToClient($client_id,json_encode(['client_id'=>$client_id]));
    }

    //消息事件
    public static function onMessage($client_id, $message)
    {

    }

    // 连接断开事件
    public static function onClose($client_id)
    {

    }

    //进程退出事件
    public static function onWebSocketConnect($client_id, $data)
    {

    }
}

操作

php artisan workman start :启动

php artisan workman stop:停止

php artisan workman restart:重启

php artisan workman reload:平滑重启

php artisan workman status:查看状态

php artisan workman start --d

 

意见反馈