Skip to content

Commit

Permalink
Add getProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Mar 23, 2021
1 parent 8f7f0a3 commit 8aec9d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Command/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
])
);
}
Expand Down
17 changes: 17 additions & 0 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/PublishHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8aec9d5

Please sign in to comment.