xiaoai/php_server/app/common/command/WorkermanServie.php

90 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace app\common\command;
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 WorkermanServie extends Command
{
protected function configure()
{
// 指令配置
$this->setName('workerman:server')
->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
->addOption('mode', 'm', Option::VALUE_OPTIONAL, 'Run the workerman server in daemon mode.')
->setDescription('Wechat server');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$output->writeln('convert start');
$action = $input->getArgument('action');
$mode = $input->getOption('mode');
// 重新构造命令行参数,以便兼容workerman的命令
global $argv;
$argv = [];
array_unshift($argv, 'think', $action);
if ($mode == 'd') {
$argv[] = '-d';
} else if ($mode == 'g') {
$argv[] = '-g';
}
try {
// 初始化Channel服务用于跨进程通信
$channel_server = new \Channel\Server('0.0.0.0', 2206);
// 在这里放心的实例化worker,
// 就像参照workerman文档写一样,
$xhsWorker = new Worker('websocket://0.0.0.0:2345');
$xhsWorker->count = 1;
$xhsWorker->name = 'IMAIWORK-XhsSocketService';
$service = new \app\common\workerman\xhs\XhsSocketService($xhsWorker);
$xhsWorker->onWorkerStart = array($service, 'onWorkerStart');
$xhsWorker->onConnect = array($service, 'onConnect');
$xhsWorker->onMessage = array($service, 'onMessage');
$xhsWorker->onClose = array($service, 'onClose');
$xhsWorker->onError = array($service, 'onError');
// 无非在workerman的文档里,代码是新建纯php文件,但在这里,写到了一个方法里.
$worker = new Worker('websocket://0.0.0.0:2347');
$worker->count = 4;
$worker->name = 'IMAIWORK-WechatSocketService';
$service = new \app\common\workerman\wechat\WechatSocketService();
$worker->onWorkerStart = array($service, 'onWorkerStart');
$worker->onConnect = array($service, 'onConnect');
$worker->onMessage = array($service, 'onMessage');
$worker->onClose = array($service, 'onClose');
$worker->onError = array($service, 'onError');
//设备socket
$tcpWorker = new Worker('tcp://0.0.0.0:6489');
$tcpWorker->count = 4;
$tcpWorker->name = 'IMAIWORK-DeviceSocketService';
$deviceService = new \app\common\workerman\wechat\DeviceSocketService();
$tcpWorker->onWorkerStart = array($deviceService, 'onWorkerStart');
$tcpWorker->onConnect = array($deviceService, 'onConnect');
$tcpWorker->onMessage = array($deviceService, 'onMessage');
$tcpWorker->onClose = array($deviceService, 'onClose');
$tcpWorker->onError = array($deviceService, 'onError');
Worker::runAll();
} catch (\Exception $e) {
// clogger($e);
print_r($e->__toString());
}
}
}