我的開發環境是基於laravel官方推薦的homestead虛擬機
首先執行 composer require workerman/workerman 安裝workerman
artisan command實現
因為workerman服務啟動是基於cli命令行模式,所以我們得用laravel的artisan來實現.
創建command
php artisan make:command WorkermanHttpserver
注冊command
App\Console\Kernel.php文件添加剛才創建的command
protected $commands = [
Commands\WorkermanHttpServer::class
];
以下例子是創建一個簡單的httpserver.其他服務請查看官方文檔.
參考官方文檔,在WorkermanHttpServer.php 中寫如下內容:
<?phpnamespace App\Console\Commands;use Illuminate\Console\Command;use Workerman\Worker;use App;class WorkermanHttpServer extends Command{protected $httpserver;/*** The name and signature of the console command.** @var string*/protected $signature = 'workerman:httpserver {action} {--daemonize}';/*** The console command description.** @var string*/protected $description = 'workerman httpserver';/*** Create a new command instance.** @return void*/public function __construct(){parent::__construct();}/*** Execute the console command.** @return mixed*/public function handle(){//因為workerman需要帶參數 所以得強制修改global $argv;$action=$this->argument('action');if(!in_array($action,['start','stop'])){$this->error('Error Arguments');exit;}$argv[0]='workerman:httpserver';$argv[1]=$action;$argv[2]=$this->option('daemonize')?'-d':'';$this->httpserver=new Worker('http://0.0.0.0:8080');// App::instance('workerman:httpserver',$this->httpserver);$this->httpserver->onMessage=function($connection,$data){$connection->send('laravel workerman hello world');};Worker::runAll();}}
運行command
php artisan workerman:httpserver start
php artisan workerman:httpserver start --daemonize //常駐后台運行
整合gatewayworker
1.安裝 Gateway-worker
由於要使用客戶端點對點通訊,選擇了 workerman/gateway-worker 的擴展包,它已經引入了 workerman/workerman 。
$ composer require workerman/gateway-worker
2.創建 Workerman 啟動文件
創建一個 artisan 命令行工具來啟動 Socket 服務端,在 app/Console/Commands 目錄下建立命令行文件。
<?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 = 'workman {action} {--d}'; protected $description = 'Start a Workerman server.'; 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(); $worker->name = 'BusinessWorker'; $worker->count = 1; $worker->registerAddress = '127.0.0.1:1236'; $worker->eventHandler = \App\Workerman\Events::class; } private function startGateWay() { $gateway = new Gateway("websocket://0.0.0.0:2346"); $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 = 