Workerman.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\push\command;
  12. use Workerman\Worker;
  13. use GatewayWorker\Register;
  14. use GatewayWorker\BusinessWorker;
  15. use GatewayWorker\Gateway;
  16. use think\console\Command;
  17. use think\console\Input;
  18. use think\console\input\Argument;
  19. use think\console\input\Option;
  20. use think\console\Output;
  21. class Workerman extends Command
  22. {
  23. /**
  24. * 配置信息
  25. * @var array
  26. */
  27. protected $config;
  28. protected function configure()
  29. {
  30. $this->setName('workerman')
  31. ->addArgument('status', Argument::OPTIONAL, "action start|stop|restart")
  32. ->addArgument('server', Argument::OPTIONAL, 'chat/channel')
  33. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  34. ->setDescription('workerman chat');
  35. }
  36. protected function init(Input $input, Output $output)
  37. {
  38. global $argv;
  39. $argv[1] = $input->getArgument('status') ?: 'start';
  40. $server = $input->getArgument('server');
  41. if ($input->hasOption('d')) {
  42. $argv[2] = '-d';
  43. } else {
  44. unset($argv[2]);
  45. }
  46. $this->config = \think\Config::get('workerman', []);
  47. return $server;
  48. }
  49. protected function execute(Input $input, Output $output)
  50. {
  51. $server = $this->init($input, $output);
  52. if (!$server || $server == 'chat') {
  53. $this->startGateWay();
  54. }
  55. if (!$server || $server == 'business') {
  56. $this->startBusinessWorker();
  57. }
  58. if (!$server || $server == 'channel') {
  59. $this->startRegister();
  60. }
  61. try {
  62. Worker::runAll();
  63. } catch (\Throwable $e) {
  64. $output->warning($e->getMessage());
  65. }
  66. }
  67. private function startBusinessWorker()
  68. {
  69. // bussinessWorker 进程
  70. $worker = new BusinessWorker();
  71. // worker名称
  72. $worker->name = $this->config['chat']['name'];
  73. // bussinessWorker进程数量
  74. $worker->count = $this->config['chat']['count'];
  75. //设置处理业务的类,此处制定Events的命名空间
  76. $worker->eventHandler = \app\push\controller\Events::class;
  77. // 服务注册地址
  78. $worker->registerAddress = $this->config['channel']['ip'] . ':' . $this->config['channel']['port'];
  79. }
  80. private function startGateWay()
  81. {
  82. $gateway = new Gateway($this->config['chat']['protocol'] . "://" . $this->config['chat']['ip'] . ":" . $this->config['chat']['port']);
  83. $gateway->name = $this->config['chat']['name'];
  84. // gateway进程数
  85. $gateway->count = $this->config['chat']['count'];
  86. $gateway->startPort = 2900;
  87. // 服务注册地址
  88. $gateway->registerAddress = $this->config['channel']['ip'] . ':' . $this->config['channel']['port'];
  89. }
  90. private function startRegister()
  91. {
  92. new Register('text://' . $this->config['text']['ip'] . ':' . $this->config['text']['port']);
  93. }
  94. }