Workerman.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace crmeb\command;
  3. use Channel\Client;
  4. use Channel\Server;
  5. use crmeb\services\SystemConfigService;
  6. use crmeb\services\workerman\chat\ChatService;
  7. use crmeb\services\workerman\WorkermanService;
  8. use think\console\Command;
  9. use think\console\Input;
  10. use think\console\input\Argument;
  11. use think\console\input\Option;
  12. use think\console\Output;
  13. use Workerman\Worker;
  14. class Workerman extends Command
  15. {
  16. /**
  17. * @var array
  18. */
  19. protected $config = [];
  20. /**
  21. * @var Worker
  22. */
  23. protected $workerServer;
  24. /**
  25. * @var Worker
  26. */
  27. protected $chatWorkerServer;
  28. /**
  29. * @var Server
  30. */
  31. protected $channelServer;
  32. /**
  33. * @var Input
  34. */
  35. public $input;
  36. /**
  37. * @var Output
  38. */
  39. public $output;
  40. protected function configure()
  41. {
  42. // 指令配置
  43. $this->setName('workerman')
  44. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  45. ->addArgument('server', Argument::OPTIONAL, 'admin/chat/channel')
  46. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  47. ->setDescription('start/stop/restart workerman');
  48. }
  49. protected function init(Input $input, Output $output)
  50. {
  51. global $argv;
  52. $argv[1] = $input->getArgument('status') ?: 'start';
  53. $server = $input->getArgument('server');
  54. if ($input->hasOption('d')) {
  55. $argv[2] = '-d';
  56. } else {
  57. unset($argv[2]);
  58. }
  59. $this->config = config('workerman');
  60. return $server;
  61. }
  62. protected function execute(Input $input, Output $output)
  63. {
  64. $server = $this->init($input, $output);
  65. $confing = SystemConfigService::more(['wss_open', 'wss_local_cert', 'wss_local_pk']);
  66. // 证书最好是申请的证书
  67. if ($confing['wss_open']) {
  68. $context = [
  69. 'ssl' => [
  70. // 请使用绝对路径
  71. 'local_cert' => realpath('public' . $confing['wss_local_cert']), // 也可以是crt文件
  72. 'local_pk' => realpath('public' . $confing['wss_local_pk']),
  73. 'verify_peer' => false,
  74. ]
  75. ];
  76. } else {
  77. $context = [];
  78. }
  79. var_dump($context);
  80. if (!$server || $server == 'admin') {
  81. var_dump('admin');
  82. //创建 admin 长连接服务
  83. $this->workerServer = new Worker($this->config['admin']['protocol'] . '://' . $this->config['admin']['ip'] . ':' . $this->config['admin']['port'], $context);
  84. $this->workerServer->count = $this->config['admin']['serverCount'];
  85. if ($confing['wss_open']) {
  86. $this->workerServer->transport = 'ssl';
  87. }
  88. }
  89. if (!$server || $server == 'chat') {
  90. var_dump('chat');
  91. //创建 h5 chat 长连接服务
  92. $this->chatWorkerServer = new Worker($this->config['chat']['protocol'] . '://' . $this->config['chat']['ip'] . ':' . $this->config['chat']['port'], $context);
  93. $this->chatWorkerServer->count = $this->config['chat']['serverCount'];
  94. if ($confing['wss_open']) {
  95. $this->chatWorkerServer->transport = 'ssl';
  96. }
  97. }
  98. if (!$server || $server == 'channel') {
  99. var_dump('channel');
  100. //创建内部通讯服务
  101. $this->channelServer = new Server($this->config['channel']['ip'], $this->config['channel']['port']);
  102. }
  103. $this->bindHandle();
  104. try {
  105. Worker::runAll();
  106. } catch (\Exception $e) {
  107. $output->warning($e->getMessage());
  108. }
  109. }
  110. protected function bindHandle()
  111. {
  112. if (!is_null($this->workerServer)) {
  113. $server = new WorkermanService($this->workerServer, $this->channelServer);
  114. // 连接时回调
  115. $this->workerServer->onConnect = [$server, 'onConnect'];
  116. // 收到客户端信息时回调
  117. $this->workerServer->onMessage = [$server, 'onMessage'];
  118. // 进程启动后的回调
  119. $this->workerServer->onWorkerStart = [$server, 'onWorkerStart'];
  120. // 断开时触发的回调
  121. $this->workerServer->onClose = [$server, 'onClose'];
  122. }
  123. if (!is_null($this->chatWorkerServer)) {
  124. $chatServer = new ChatService($this->chatWorkerServer, $this->channelServer);
  125. $this->chatWorkerServer->onConnect = [$chatServer, 'onConnect'];
  126. $this->chatWorkerServer->onMessage = [$chatServer, 'onMessage'];
  127. $this->chatWorkerServer->onWorkerStart = [$chatServer, 'onWorkerStart'];
  128. $this->chatWorkerServer->onClose = [$chatServer, 'onClose'];
  129. }
  130. }
  131. }