zoukankan      html  css  js  c++  java
  • [SF] Symfony 在 console 中结合 Workerman

    在web框架的console中,命令不再是直接指定入口文件,如以往 php test.php start,而是类似 php app/console do 的形式。

    workerman 对命令的解析是 parseCommand 方法,里面主要是处理 $argv 全局变量。

    那么我们只需要在自己的逻辑中对其重新赋值,满足 $argv[1] 是动作 start | stop | restart | ... 即可,那么剩余workerman参数就是 $argv[2],依次类推。

    Symfony2 command:

    namespace AppBundleCommand;
    
    use SymfonyComponentConsoleInputInputArgument;
    use SymfonyComponentConsoleInputInputInterface;
    use SymfonyComponentConsoleOutputOutputInterface;
    
    use WorkermanConnectionTcpConnection;
    use WorkermanWorker;
    
    /**
     * @author farwish <farwish(a)foxmail.com>
     *
     * Class DataWorkerCommand
     * @package AppBundleCommand
     */
    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. 

    Link: http://www.cnblogs.com/farwish/p/7988617.html

  • 相关阅读:
    图->存储结构->十字链表
    图->存储结构->邻接表
    P2278-[HNOI2003]操作系统
    P1801-黑匣子_NOI导刊2010提高(06)
    P1197-[JSOI2008]星球大战
    P2024- [NOI2001]食物链
    P1111-修复公路
    ACM模板——二分图匹配
    P2055-[ZJOI2009]假期的宿舍
    ACM模板——强连通分量
  • 原文地址:https://www.cnblogs.com/farwish/p/7988617.html
Copyright © 2011-2022 走看看