当前位置:首页 > 开发 > 正文内容

Laravel中Websocket基本使用(Workerman)

hackcode2023年08月26日 14:32开发538


1.安装gateway-worker

  • composer require workerman/gateway-worker

2.创建命令

  • php artisan make:command WorkermanCommand

3.创建的命令存放在 app\Console\Commands 在WorkermanCommand.php键入如下代码


  • <?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 'workerman
  •     {action : action}
  •     {--start=all : start}
  •     {--d : daemon mode}';
  •     protected $description 'Start a Workerman server.';
  •     public function handle()
  •     {
  •         global $argv;
  •         $action $this->argument('action');
  •         /**
  •          * 针对 Windows 一次执行,无法注册多个协议的特殊处理
  •          */
  •         if ($action === 'single') {
  •             $start $this->option('start');
  •             if ($start === 'register') {
  •                 $this->startRegister();
  •             } elseif ($start === 'gateway') {
  •                 $this->startGateWay();
  •             } elseif ($start === 'worker') {
  •                 $this->startBusinessWorker();
  •             }
  •             Worker::runAll();
  •             return;
  •         }
  •         $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();
  •         $worker->name            = 'BusinessWorker';
  •         $worker->count           = 1;
  •         $worker->registerAddress = '127.0.0.1:6001';
  •         $worker->eventHandler    = \App\Workerman\Events::class;
  •     }
  •     private function startGateWay()
  •     {
  •         $gateway new Gateway("websocket://0.0.0.0:6002");
  •         $gateway->name                 = 'Gateway';
  •         $gateway->count                = 1;
  •         $gateway->lanIp                = '127.0.0.1';
  •         $gateway->startPort            = 2300;
  •         $gateway->pingInterval         = 30;
  •         $gateway->pingNotResponseLimit = 0;
  •         $gateway->pingData             = '{"type":"@heart@"}';
  •         $gateway->registerAddress      = '127.0.0.1:6001';
  •     }
  •     private function startRegister()
  •     {
  •         new Register('text://0.0.0.0:6001');
  •     }
  • }

创建bat文件用于win启动,win无法支持同时启动多个work所以会卡住

  • start /b php artisan workerman single --start=register
  • start /b php artisan workerman single --start=gateway
  • start /b php artisan workerman single --start=worker
  • pause

4.创建事件监听文件来监听处理 workman 的各种事件。 创建 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)
  •     {
  •         // 向当前client_id发送数据
  •         Gateway::sendToClient($client_id"Hello $client_id");
  •     }
  •     //进程退出事件
  •     public static function onWebSocketConnect($client_id$data)
  •     {
  •     }
  •     //消息事件
  •     public static function onMessage($client_id$message)
  •     {
  •          // 向所有人发送
  •          Gateway::sendToAll("$client_id said $message");
  •     }
  •     // 连接断开事件
  •     public static function onClose($client_id)
  •     {
  •     }
  • }
返回列表

上一篇:JWT

下一篇:axios下载文件