Workerman.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\command;
  12. use app\services\system\config\SystemConfigServices;
  13. use Channel\Server;
  14. use crmeb\services\workerman\chat\ChatService;
  15. use crmeb\services\workerman\WorkermanService;
  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. use Workerman\Worker;
  22. class Workerman extends Command
  23. {
  24. /**
  25. * @var array
  26. */
  27. protected $config = [];
  28. /**
  29. * @var Worker
  30. */
  31. protected $workerServer;
  32. /**
  33. * @var Worker
  34. */
  35. protected $chatWorkerServer;
  36. /**
  37. * @var Server
  38. */
  39. protected $channelServer;
  40. /**
  41. * @var Input
  42. */
  43. public $input;
  44. /**
  45. * @var Output
  46. */
  47. public $output;
  48. protected function configure()
  49. {
  50. // 指令配置
  51. $this->setName('workerman')
  52. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  53. ->addArgument('server', Argument::OPTIONAL, 'admin/chat/channel')
  54. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  55. ->setDescription('start/stop/restart workerman');
  56. }
  57. protected function init(Input $input, Output $output)
  58. {
  59. global $argv;
  60. $argv[1] = $input->getArgument('status') ?: 'start';
  61. $server = $input->getArgument('server');
  62. if ($input->hasOption('d')) {
  63. $argv[2] = '-d';
  64. } else {
  65. unset($argv[2]);
  66. }
  67. $this->config = config('workerman');
  68. return $server;
  69. }
  70. protected function execute(Input $input, Output $output)
  71. {
  72. $server = $this->init($input, $output);
  73. /** @var SystemConfigServices $services */
  74. $services = app()->make(SystemConfigServices::class);
  75. $sslConfig = $services->getSslFilePath();
  76. // $confing['wss_open'] = $sslConfig['wssOpen'] ?? 0;
  77. $confing['wss_open'] = 0;
  78. $confing['wss_local_cert'] = $sslConfig['wssLocalCert'] ?? '';
  79. $confing['wss_local_pk'] = $sslConfig['wssLocalpk'] ?? '';
  80. // 证书最好是申请的证书
  81. if ($confing['wss_open']) {
  82. $context = [
  83. 'ssl' => [
  84. // 请使用绝对路径
  85. 'local_cert' => realpath('public' . $confing['wss_local_cert']), // 也可以是crt文件
  86. 'local_pk' => realpath('public' . $confing['wss_local_pk']),
  87. 'verify_peer' => false,
  88. ]
  89. ];
  90. } else {
  91. $context = [];
  92. }
  93. Worker::$pidFile = app()->getRootPath() . 'runtime/workerman.pid';
  94. if (!$server || $server == 'admin') {
  95. var_dump('admin');
  96. //创建 admin 长连接服务
  97. $this->workerServer = new Worker($this->config['admin']['protocol'] . '://' . $this->config['admin']['ip'] . ':' . $this->config['admin']['port'], $context);
  98. $this->workerServer->count = $this->config['admin']['serverCount'];
  99. if ($confing['wss_open']) {
  100. $this->workerServer->transport = 'ssl';
  101. }
  102. }
  103. if (!$server || $server == 'chat') {
  104. var_dump('chat');
  105. //创建 h5 chat 长连接服务
  106. $this->chatWorkerServer = new Worker($this->config['chat']['protocol'] . '://' . $this->config['chat']['ip'] . ':' . $this->config['chat']['port'], $context);
  107. $this->chatWorkerServer->count = $this->config['chat']['serverCount'];
  108. if ($confing['wss_open']) {
  109. $this->chatWorkerServer->transport = 'ssl';
  110. }
  111. }
  112. if (!$server || $server == 'channel') {
  113. var_dump('channel');
  114. //创建内部通讯服务
  115. $this->channelServer = new Server($this->config['channel']['ip'], $this->config['channel']['port']);
  116. }
  117. $this->bindHandle();
  118. try {
  119. Worker::runAll();
  120. } catch (\Exception $e) {
  121. $output->warning($e->getMessage());
  122. }
  123. }
  124. protected function bindHandle()
  125. {
  126. if (!is_null($this->workerServer)) {
  127. $server = new WorkermanService($this->workerServer, $this->channelServer);
  128. // 连接时回调
  129. $this->workerServer->onConnect = [$server, 'onConnect'];
  130. // 收到客户端信息时回调
  131. $this->workerServer->onMessage = [$server, 'onMessage'];
  132. // 进程启动后的回调
  133. $this->workerServer->onWorkerStart = [$server, 'onWorkerStart'];
  134. // 断开时触发的回调
  135. $this->workerServer->onClose = [$server, 'onClose'];
  136. }
  137. if (!is_null($this->chatWorkerServer)) {
  138. $chatServer = new ChatService($this->chatWorkerServer, $this->channelServer);
  139. $this->chatWorkerServer->onConnect = [$chatServer, 'onConnect'];
  140. $this->chatWorkerServer->onMessage = [$chatServer, 'onMessage'];
  141. $this->chatWorkerServer->onWorkerStart = [$chatServer, 'onWorkerStart'];
  142. $this->chatWorkerServer->onClose = [$chatServer, 'onClose'];
  143. }
  144. }
  145. }