在web框架的console中,命令再也不是直接指定入口文件,如以往 php test.php start,而是相似 php app/console do 的形式。php
workerman 對命令的解析是 parseCommand 方法,裏面主要是處理 $argv 全局變量。html
那麼咱們只須要在本身的邏輯中對其從新賦值,知足 $argv[1] 是動做 start | stop | restart | ... 便可,那麼剩餘workerman參數就是 $argv[2],依次類推。web
Symfony2 command:websocket
namespace AppBundle\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Workerman\Connection\TcpConnection; use Workerman\Worker; /** * @author farwish <farwish(a)foxmail.com> * * Class DataWorkerCommand * @package AppBundle\Command */ class DataWorkerCommand extends BaseCommand { public function configure() { $this->setName('xc:data:worker') ->setDescription('啓動服務') ->addArgument( 'subcommands', InputArgument::IS_ARRAY, '可選多個參數' ) ; } /** * app/console xc:data:worker start d [g] * app/console xc:data:worker stop * app/console xc:data:worker status * app/console xc:data:worker restart d [g] * * @param InputInterface $input * @param OutputInterface $output */ public function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); global $argv; /* Original data like Array ( [0] => worker.php [1] => start [2] => -d [3] => -g ) */ /* Console data like Array ( [0] => app/console [1] => xc:data:worker [2] => start [3] => d [4] => g ) */ // So redefine arguments if (isset($argv[2])) { $argv[1] = $argv[2]; if (isset($argv[3])) { $argv[2] = "-{$argv[3]}"; if (isset($argv[4])) { $argv[3] = "-{$argv[4]}"; } else { unset($argv[3]); } } else { unset($argv[2]); } } // worker $worker = new Worker("websocket://0.0.0.0:9000"); $worker->count = 4; $worker->onMessage = function ($connection, $data) { /* @var TcpConnection $connection */ $connection->send('hello'); }; Worker::runAll(); } }
Thats all. app