From 8aec9d5cdaebb8c80468f93cebb885fe678eda9d Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Tue, 23 Mar 2021 16:22:28 +0800 Subject: [PATCH] Add getProperties --- src/Command/PublishCommand.php | 1 + src/Handler/AbstractHandler.php | 17 +++++++++++++++++ src/Handler/PublishHandler.php | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Command/PublishCommand.php b/src/Command/PublishCommand.php index 265d7aa..6ad8172 100644 --- a/src/Command/PublishCommand.php +++ b/src/Command/PublishCommand.php @@ -49,6 +49,7 @@ protected function configure() new InputOption('will-retain', null, InputOption::VALUE_OPTIONAL, 'If given, if the client disconnects unexpectedly the message sent out will be treated as a retained message', 0), new InputOption('ssl', 'S', InputOption::VALUE_OPTIONAL, 'Enable SSL encryption', false), new InputOption('config-path', null, InputOption::VALUE_OPTIONAL, 'Setting the Swoole config file path'), + new InputOption('properties-path', null, InputOption::VALUE_OPTIONAL, 'Setting the Properties config file path'), ]) ); } diff --git a/src/Handler/AbstractHandler.php b/src/Handler/AbstractHandler.php index d5a87d0..04cf041 100644 --- a/src/Handler/AbstractHandler.php +++ b/src/Handler/AbstractHandler.php @@ -149,6 +149,7 @@ public function getConnectConfig(): ClientConfig ->setProtocolLevel($this->getProtocolLevel()) ->setSockType($this->getSocketType()) ->setSwooleConfig($this->getSwooleConfig()) + ->setProperties($this->getProperties('connect')) ->setMaxAttempts(0); // Disable auto reconnection } @@ -184,6 +185,22 @@ public function getSwooleConfig(): array return []; } + public function getProperties(string $key = ''): array + { + $path = $this->input->getOption('properties-path'); + if ($path && is_file($path)) { + $content = include $path; + + if ($key) { + return $content[$key] ?? []; + } + + return $content; + } + + return []; + } + protected function log($msg): void { $date = date('Y-m-d H:i:s'); diff --git a/src/Handler/PublishHandler.php b/src/Handler/PublishHandler.php index 698c60c..176384b 100644 --- a/src/Handler/PublishHandler.php +++ b/src/Handler/PublishHandler.php @@ -41,7 +41,7 @@ public function handle(InputInterface $input, OutputInterface $output): int if ($this->getConnectConfig()->isMQTT5()) { $this->logInfo("Connect Reason Code: {$connect['code']}, Reason: " . ReasonCode::getReasonPhrase($connect['code'])); } - $publish = $client->publish($topic, $message, $this->getQos(), $this->getDup(), $this->getRetain()); + $publish = $client->publish($topic, $message, $this->getQos(), $this->getDup(), $this->getRetain(), $this->getProperties('publish')); } catch (\Throwable $e) { $this->logError('Publish error'); goto failure;