|
|
@@ -0,0 +1,98 @@
|
|
|
+<?php
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | Workerman 命令行入口
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+namespace app\command;
|
|
|
+
|
|
|
+use Channel\Server;
|
|
|
+use think\console\Command;
|
|
|
+use think\console\Input;
|
|
|
+use think\console\input\Argument;
|
|
|
+use think\console\input\Option;
|
|
|
+use think\console\Output;
|
|
|
+use Workerman\Worker;
|
|
|
+
|
|
|
+class Workerman extends Command
|
|
|
+{
|
|
|
+ protected $config = [];
|
|
|
+ protected $workerServer;
|
|
|
+ protected $chatWorkerServer;
|
|
|
+ protected $channelServer;
|
|
|
+
|
|
|
+ protected function configure()
|
|
|
+ {
|
|
|
+ $this->setName('workerman')
|
|
|
+ ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status')
|
|
|
+ ->addArgument('server', Argument::OPTIONAL, 'admin/chat/channel')
|
|
|
+ ->addOption('d', null, Option::VALUE_NONE, '守护进程方式启动')
|
|
|
+ ->setDescription('Workerman服务管理命令');
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(Input $input, Output $output)
|
|
|
+ {
|
|
|
+ global $argv;
|
|
|
+ $argv[1] = $input->getArgument('status') ?: 'start';
|
|
|
+ $server = $input->getArgument('server');
|
|
|
+
|
|
|
+ if ($input->hasOption('d')) {
|
|
|
+ $argv[2] = '-d';
|
|
|
+ } else {
|
|
|
+ unset($argv[2]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->config = config('workerman');
|
|
|
+ Worker::$pidFile = app()->getRootPath() . 'runtime/workerman.pid';
|
|
|
+
|
|
|
+ if (!$server || $server == 'admin') {
|
|
|
+ $output->info('启动 Admin 服务: 0.0.0.0:' . $this->config['admin']['port']);
|
|
|
+ $this->workerServer = new Worker(
|
|
|
+ $this->config['admin']['protocol'] . '://' .
|
|
|
+ $this->config['admin']['ip'] . ':' .
|
|
|
+ $this->config['admin']['port']
|
|
|
+ );
|
|
|
+ $this->workerServer->count = $this->config['admin']['serverCount'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$server || $server == 'chat') {
|
|
|
+ $output->info('启动 Chat 服务: 0.0.0.0:' . $this->config['chat']['port']);
|
|
|
+ $this->chatWorkerServer = new Worker(
|
|
|
+ $this->config['chat']['protocol'] . '://' .
|
|
|
+ $this->config['chat']['ip'] . ':' .
|
|
|
+ $this->config['chat']['port']
|
|
|
+ );
|
|
|
+ $this->chatWorkerServer->count = $this->config['chat']['serverCount'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$server || $server == 'channel') {
|
|
|
+ $output->info('启动 Channel 服务: ' . $this->config['channel']['ip'] . ':' . $this->config['channel']['port']);
|
|
|
+ $this->channelServer = new Server($this->config['channel']['ip'], $this->config['channel']['port']);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->bindHandle();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Worker::runAll();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $output->warning($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function bindHandle()
|
|
|
+ {
|
|
|
+ if (!is_null($this->workerServer)) {
|
|
|
+ $server = new \app\services\workerman\WorkermanService($this->workerServer, $this->channelServer);
|
|
|
+ $this->workerServer->onConnect = [$server, 'onConnect'];
|
|
|
+ $this->workerServer->onMessage = [$server, 'onMessage'];
|
|
|
+ $this->workerServer->onWorkerStart = [$server, 'onWorkerStart'];
|
|
|
+ $this->workerServer->onClose = [$server, 'onClose'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_null($this->chatWorkerServer)) {
|
|
|
+ $chatServer = new \app\services\workerman\chat\ChatService($this->chatWorkerServer, $this->channelServer);
|
|
|
+ $this->chatWorkerServer->onConnect = [$chatServer, 'onConnect'];
|
|
|
+ $this->chatWorkerServer->onMessage = [$chatServer, 'onMessage'];
|
|
|
+ $this->chatWorkerServer->onWorkerStart = [$chatServer, 'onWorkerStart'];
|
|
|
+ $this->chatWorkerServer->onClose = [$chatServer, 'onClose'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|