Workerman.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace crmeb\command;
  3. use Channel\Client;
  4. use Channel\Server;
  5. use crmeb\services\workerman\chat\ChatService;
  6. use crmeb\services\workerman\WorkermanService;
  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. /**
  16. * @var array
  17. */
  18. protected $config = [];
  19. /**
  20. * @var Worker
  21. */
  22. protected $workerServer;
  23. /**
  24. * @var Worker
  25. */
  26. protected $chatWorkerServer;
  27. /**
  28. * @var Server
  29. */
  30. protected $channelServer;
  31. /**
  32. * @var Input
  33. */
  34. public $input;
  35. /**
  36. * @var Output
  37. */
  38. public $output;
  39. protected function configure()
  40. {
  41. // 指令配置
  42. $this->setName('workerman')
  43. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  44. ->addArgument('server', Argument::OPTIONAL, 'admin/chat/channel')
  45. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  46. ->setDescription('start/stop/restart workerman');
  47. }
  48. protected function init(Input $input, Output $output)
  49. {
  50. global $argv;
  51. $argv[1] = $input->getArgument('status') ?: 'start';
  52. $server = $input->getArgument('server');
  53. if ($input->hasOption('d')) {
  54. $argv[2] = '-d';
  55. } else {
  56. unset($argv[2]);
  57. }
  58. $this->config = config('workerman');
  59. return $server;
  60. }
  61. protected function execute(Input $input, Output $output)
  62. {
  63. $server = $this->init($input, $output);
  64. Worker::$pidFile = app()->getRootPath().'workerman.pid';
  65. if(!$server || $server == 'admin'){
  66. var_dump('admin');
  67. //创建 admin 长连接服务
  68. $this->workerServer = new Worker($this->config['admin']['protocol'] . '://' . $this->config['admin']['ip'] . ':' . $this->config['admin']['port']);
  69. $this->workerServer->count = $this->config['admin']['serverCount'];
  70. }
  71. if(!$server || $server == 'chat') {
  72. var_dump('chat');
  73. //创建 h5 chat 长连接服务
  74. $this->chatWorkerServer = new Worker($this->config['chat']['protocol'] . '://' . $this->config['chat']['ip'] . ':' . $this->config['chat']['port']);
  75. $this->chatWorkerServer->count = $this->config['chat']['serverCount'];
  76. }
  77. if(!$server || $server == 'channel') {
  78. var_dump('channel');
  79. //创建内部通讯服务
  80. $this->channelServer = new Server($this->config['channel']['ip'], $this->config['channel']['port']);
  81. }
  82. $this->bindHandle();
  83. try {
  84. Worker::runAll();
  85. } catch (\Exception $e) {
  86. $output->warning($e->getMessage());
  87. }
  88. }
  89. protected function bindHandle()
  90. {
  91. if(!is_null($this->workerServer)){
  92. $server = new WorkermanService($this->workerServer, $this->channelServer);
  93. // 连接时回调
  94. $this->workerServer->onConnect = [$server, 'onConnect'];
  95. // 收到客户端信息时回调
  96. $this->workerServer->onMessage = [$server, 'onMessage'];
  97. // 进程启动后的回调
  98. $this->workerServer->onWorkerStart = [$server, 'onWorkerStart'];
  99. // 断开时触发的回调
  100. $this->workerServer->onClose = [$server, 'onClose'];
  101. }
  102. if(!is_null($this->chatWorkerServer)) {
  103. $chatServer = new ChatService($this->chatWorkerServer, $this->channelServer);
  104. $this->chatWorkerServer->onConnect = [$chatServer, 'onConnect'];
  105. $this->chatWorkerServer->onMessage = [$chatServer, 'onMessage'];
  106. $this->chatWorkerServer->onWorkerStart = [$chatServer, 'onWorkerStart'];
  107. $this->chatWorkerServer->onClose = [$chatServer, 'onClose'];
  108. }
  109. }
  110. }