laravel gateway-worker
1.php artisan make:command GatewayWorkerServer
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use GatewayWorker\BusinessWorker; use GatewayWorker\Gateway; use GatewayWorker\Register; use Illuminate\Support\Facades\Log; use Workerman\Worker; use App\GatewayWorker\Events; class GatewayWorkerServer extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'gateway-worker {action : action} {--start=all : start} {--d : daemon mode}'; /** * 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 mixed */ 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] 默认是,当前文件,可以不修改 */ //$argv[0] = 'wk'; $argv[1] = $action; // 控制是否进入 daemon 模式 $argv[2] = $this->option('d') ? '-d' : ''; $this->start(); } private function start() { Log::info('test'); $this->startGateWay(); $this->startBusinessWorker(); $this->startRegister(); } private function startBusinessWorker() { $worker = new BusinessWorker(); $worker->name = 'BusinessWorker'; $worker->count = 4; $worker->registerAddress = '127.0.0.1:1238'; $worker->eventHandler = Events::class; } private function startGateWay() { $gateway = new Gateway("websocket://0.0.0.0:8282"); $gateway->name = 'Gateway'; $gateway->count = 4; $gateway->lanIp = '127.0.0.1'; $gateway->startPort = 2900; // $gateway->pingInterval = 30; // $gateway->pingNotResponseLimit = 0; // $gateway->pingData = '{"mode":"heart"}'; $gateway->registerAddress = '127.0.0.1:1238'; } private function startRegister() { new Register('text://0.0.0.0:1238'); } }
2.添加事件监听文件
<?php namespace App\GatewayWorker; use GatewayWorker\Lib\Gateway; use Illuminate\Support\Facades\Log; class Events { public static function onWorkerStart($businessWorker) { Log::info('onWorkerStart'); echo "onWorkerStart\r\n"; } public static function onConnect($client_id) { Log::info('onConnect'); Gateway::sendToClient($client_id, json_encode(['type' => 'onConnect', 'client_id' => $client_id])); echo "onConnect\r\n"; } public static function onWebSocketConnect($client_id, $data) { Log::info('onWebSocketConnect'); echo "onWebSocketConnect\r\n"; } public static function onMessage($client_id, $message) { Log::info('onMessage'); Gateway::sendToClient($client_id, json_encode(['type' => 'onMessage', 'client_id' => $client_id, 'name' => json_decode($message)->name])); echo "onMessage\r\n"; } public static function onClose($client_id) { Log::info('Workerman close connection' . $client_id); echo "onClose\r\n"; } }
3.根目录创建文件 start_for_win.bat(windows不支持多线程)
start /b php artisan gateway-worker single --start=register start /b php artisan gateway-worker single --start=gateway start /b php artisan gateway-worker single --start=worker pause