laravel 中使用workerman

    技术2022-07-11  121

    <?php namespace App\Console\Commands; use App\Handler\Danger\Danger; use App\Handler\SendToClient; use Illuminate\Console\Command; use Illuminate\Support\Facades\App; use PHPSocketIO\SocketIO; use Workerman\Autoloader; use Workerman\Connection\AsyncTcpConnection; use Workerman\Task\Task; use Workerman\Worker; class Sockets extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'socketio {action}{--d}'; /** * The console command description. * * @var string */ protected $description = 'Start a SocketIO server.'; /** * 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('d') ? '-d' : '';//该参数是以daemon(守护进程)方式启动 // 创建一个Worker监听2345端口,使用websocket协议通讯 $io = new SocketIO(3122); //$handler = App::make('Handler\RockHandler'); $io->on('workerStart', function ()use($io){ new Task($io); }); $io->on('connection', function ($socket) { $socket->emit('信息1','wtrewtwrt'); }); // 运行worker Worker::runAll(); } }

    最后运行 php artisan socketio start

    Processed: 0.048, SQL: 9