laravel6-workerman
1.安装扩展包
composer require workerman/gateway-worker composer require workerman/gatewayclient
2.生成命令文件
php artisan make:command Workerman
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Workerman\Worker; class Workerman extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'workerman {action} {--daemonize}'; /** * 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. * * @return mixed */ public function handle() { global $argv;//定义全局变量 $arg = $this->argument('action'); $argv[1] = $arg; $argv[2] = $this->option('daemonize') ? '-d' : '';//该参数是以daemon(守护进程)方式启动 global $text_worker; // 创建一个Worker监听2345端口,使用websocket协议通讯 $text_worker = new Worker("websocket://0.0.0.0:2345"); $text_worker->uidConnections = array();//在线用户连接对象 $text_worker->uidInfo = array();//在线用户的用户信息 // 启动4个进程对外提供服务 $text_worker->count = 4; //当启动workerman的时候 触发此方法 $text_worker->onWorkerStart = function () { }; //当浏览器连接的时候触发此函数 $text_worker->onConnect = function ($connection) { }; //向用户发送信息的时候触发 //$connection 当前连接的人的信息 $data 发送的数据 $text_worker->onMessage = function ($connection, $data) { }; //浏览器断开链接的时候触发 $text_worker->onClose = function ($connection) { }; Worker::runAll(); } }
3.启动workerman
php artisan workerman start