Workerman.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Workerman 命令行入口
  4. // +----------------------------------------------------------------------
  5. namespace app\command;
  6. use Channel\Server;
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\input\Argument;
  10. use think\console\input\Option;
  11. use think\console\Output;
  12. use Workerman\Worker;
  13. class Workerman extends Command
  14. {
  15. protected $config = [];
  16. protected $workerServer;
  17. protected $chatWorkerServer;
  18. protected $channelServer;
  19. protected function configure()
  20. {
  21. $this->setName('workerman')
  22. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status')
  23. ->addArgument('server', Argument::OPTIONAL, 'admin/chat/channel')
  24. ->addOption('d', null, Option::VALUE_NONE, '守护进程方式启动')
  25. ->setDescription('Workerman服务管理命令');
  26. }
  27. protected function execute(Input $input, Output $output)
  28. {
  29. global $argv;
  30. $argv[1] = $input->getArgument('status') ?: 'start';
  31. $server = $input->getArgument('server');
  32. if ($input->hasOption('d')) {
  33. $argv[2] = '-d';
  34. } else {
  35. unset($argv[2]);
  36. }
  37. $this->config = config('workerman');
  38. Worker::$pidFile = app()->getRootPath() . 'runtime/workerman.pid';
  39. if (!$server || $server == 'admin') {
  40. $output->info('启动 Admin 服务: 0.0.0.0:' . $this->config['admin']['port']);
  41. $this->workerServer = new Worker(
  42. $this->config['admin']['protocol'] . '://' .
  43. $this->config['admin']['ip'] . ':' .
  44. $this->config['admin']['port']
  45. );
  46. $this->workerServer->count = $this->config['admin']['serverCount'];
  47. }
  48. if (!$server || $server == 'chat') {
  49. $output->info('启动 Chat 服务: 0.0.0.0:' . $this->config['chat']['port']);
  50. $this->chatWorkerServer = new Worker(
  51. $this->config['chat']['protocol'] . '://' .
  52. $this->config['chat']['ip'] . ':' .
  53. $this->config['chat']['port']
  54. );
  55. $this->chatWorkerServer->count = $this->config['chat']['serverCount'];
  56. }
  57. if (!$server || $server == 'channel') {
  58. $output->info('启动 Channel 服务: ' . $this->config['channel']['ip'] . ':' . $this->config['channel']['port']);
  59. $this->channelServer = new Server($this->config['channel']['ip'], $this->config['channel']['port']);
  60. }
  61. $this->bindHandle();
  62. try {
  63. Worker::runAll();
  64. } catch (\Exception $e) {
  65. $output->warning($e->getMessage());
  66. }
  67. }
  68. protected function bindHandle()
  69. {
  70. if (!is_null($this->workerServer)) {
  71. $server = new \app\services\workerman\WorkermanService($this->workerServer, $this->channelServer);
  72. $this->workerServer->onConnect = [$server, 'onConnect'];
  73. $this->workerServer->onMessage = [$server, 'onMessage'];
  74. $this->workerServer->onWorkerStart = [$server, 'onWorkerStart'];
  75. $this->workerServer->onClose = [$server, 'onClose'];
  76. }
  77. if (!is_null($this->chatWorkerServer)) {
  78. $chatServer = new \app\services\workerman\chat\ChatService($this->chatWorkerServer, $this->channelServer);
  79. $this->chatWorkerServer->onConnect = [$chatServer, 'onConnect'];
  80. $this->chatWorkerServer->onMessage = [$chatServer, 'onMessage'];
  81. $this->chatWorkerServer->onWorkerStart = [$chatServer, 'onWorkerStart'];
  82. $this->chatWorkerServer->onClose = [$chatServer, 'onClose'];
  83. }
  84. }
  85. }