74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace app\command\admin;
|
|
|
|
|
|
use app\service\admin\LoginService;
|
|
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 as WmWorker;
|
|
|
|
class Worker extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
$this->setName('admin:worker')
|
|
->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('后台系统worker服务');
|
|
}
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
{
|
|
$output->writeln('convert start');
|
|
|
|
$action = $input->getArgument('action');
|
|
$mode = $input->getOption('mode');
|
|
|
|
|
|
global $argv;
|
|
|
|
$argv = [];
|
|
|
|
array_unshift($argv, 'think', $action);
|
|
if ($mode == 'd') {
|
|
$argv[] = '-d';
|
|
} else if ($mode == 'g') {
|
|
$argv[] = '-g';
|
|
}
|
|
|
|
/*
|
|
* 创建后台ws链接
|
|
*/
|
|
$adminWorker = new WmWorker('websocket://0.0.0.0:19980');
|
|
$adminWorker->onMessage = function ($connection, $message) {
|
|
var_dump($message);
|
|
$this->app->event->trigger('admin.websocket.Open', $connection, $message);
|
|
$messageJson = json_decode($message, true);
|
|
if($messageJson) {
|
|
if($messageJson['event'] == 'bind_connect_id') {
|
|
$authorization = $messageJson['data']['token'] ?? '';
|
|
$authorization = str_replace('Bearer ', '', $authorization);
|
|
$loginSrv = new LoginService();
|
|
$auth = $loginSrv->checkUserAccessToken($authorization);
|
|
var_dump("当前用户: ".$auth->userId);
|
|
$connection->userId = $auth->userId;
|
|
$connection->send(json_encode(['event'=>'bind_connect_id_success','userId'=>$auth->userId]));
|
|
}
|
|
if ($messageJson['event'] == 'lock_client_screen') {
|
|
if($connection->userId) {
|
|
var_dump("锁定客户端/通知全部ws触发锁定操作");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
$adminWorker->onClose = function ($connection) {
|
|
$this->app->event->trigger('admin.websocket.Close', $connection);
|
|
};
|
|
|
|
WmWorker::runAll();
|
|
}
|
|
} |