18 Commits

Author SHA1 Message Date
a243473d24 修改示例包地址 2023-07-06 14:33:27 +08:00
d5ded08f5e 更新composer文件 2023-07-06 11:29:10 +08:00
dfa86d9dc8 更新README文件 2023-07-06 11:27:46 +08:00
0100c86442 修改包信息 2023-07-06 11:20:58 +08:00
bbaff88e41 修改包名称 2023-07-06 11:16:41 +08:00
42227c7e56 修改包名 2023-07-06 11:07:38 +08:00
30040e8503 Replace deprecated set-output in GHA 2023-05-15 20:14:23 +02:00
68c11c6b4a Fix phpcs in ConnectionManager 2023-05-15 20:10:38 +02:00
b059586d08 Allow broadcasting to a specific log channel (#40)
* Allow broadcasting to a specific log channel

* Fix  check condition

* Update src/ConnectionManager.php

* Update config/mqtt-client.php

---------

Co-authored-by: Namoshek <Namoshek@users.noreply.github.com>
2023-05-15 20:04:29 +02:00
0669643386 Update minimum php-mqtt/client version (#39)
Versions before 1.3.0 don't have setReconnectAutomatically
2023-04-05 18:41:07 +02:00
936ce0549d GHA: Run tests against PHP 8.2 (#37) 2023-02-14 20:50:15 +01:00
c42ec1d6ff Laravel 10.x Compatibility (#35)
* Bump dependencies for Laravel 10

* Update version constraints in composer.json

---------

Co-authored-by: Marvin Mall <marvin-mall@msn.com>
2023-02-14 20:41:59 +01:00
d2a2a839e4 Fix: PR target event allows external builds (#32) 2022-11-22 22:32:07 +01:00
fe0cb883e7 Add auto reconnect support (#31)
* Update mqtt-client.php

* Update ConnectionManager.php

* Update mqtt-client.php

* Update mqtt-client.php

* Update mqtt-client.php

* Update ConnectionManager.php

* Update mqtt-client.php

* Add and normalize auto-reconnect settings

* Add .gitkeep

* Remove .gitkeep

Co-authored-by: Marvin Mall <marvin-mall@msn.com>
2022-11-22 22:26:38 +01:00
136ce2cb75 Bump actions/checkout from 2 to 3 (#24)
Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-04-10 15:50:12 +02:00
c398d5ec07 Add Dependabot and improve build pipeline (#23)
* Add dependabot config

* Update actions/cache and simplify variable usage in build
2022-04-10 15:45:17 +02:00
d237909b6d Feature laravel 9 (#22)
* Support Laravel 9

* Build and test using PHP 8.1
2022-02-10 19:22:40 +01:00
7dfaac06e4 Fix: Recreate disconnected connections when being retrieved (#19) 2022-01-08 14:23:13 +01:00
6 changed files with 112 additions and 60 deletions

23
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,23 @@
version: 2
updates:
- package-ecosystem: "composer"
directory: "/"
allow:
- dependency-type: "development"
schedule:
interval: "daily"
time: "05:00"
timezone: "Europe/Vienna"
labels:
- "composer dependencies"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "05:00"
timezone: "Europe/Vienna"
labels:
- "github actions"

View File

@ -4,7 +4,7 @@ on:
push: push:
branches: branches:
- master - master
pull_request: pull_request_target:
types: [opened, synchronize, reopened] types: [opened, synchronize, reopened]
jobs: jobs:
@ -15,10 +15,13 @@ jobs:
strategy: strategy:
matrix: matrix:
php-version: ['7.4', '8.0'] php-version: ['7.4', '8.0', '8.1', '8.2']
include:
- php-version: '8.2'
run-sonarqube-analysis: true
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v3
with: with:
fetch-depth: 0 fetch-depth: 0
@ -38,10 +41,10 @@ jobs:
- name: Get Composer Cache Directory - name: Get Composer Cache Directory
id: composer-cache id: composer-cache
run: | run: |
echo "::set-output name=dir::$(composer config cache-files-dir)" echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache Composer dependencies - name: Cache Composer dependencies
uses: actions/cache@v2 uses: actions/cache@v3
with: with:
path: ${{ steps.composer-cache.outputs.dir }} path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
@ -55,7 +58,7 @@ jobs:
- name: Run SonarQube analysis - name: Run SonarQube analysis
uses: sonarsource/sonarcloud-github-action@master uses: sonarsource/sonarcloud-github-action@master
if: matrix.php-version == '8.0' if: matrix.run-sonarqube-analysis
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }} SONAR_TOKEN: ${{ secrets.SONARCLOUD_TOKEN }}

View File

@ -1,4 +1,4 @@
# php-mqtt/laravel-client # ykxiao/laravel-mqtt-client
[![Latest Stable Version](https://poser.pugx.org/php-mqtt/laravel-client/v)](https://packagist.org/packages/php-mqtt/laravel-client) [![Latest Stable Version](https://poser.pugx.org/php-mqtt/laravel-client/v)](https://packagist.org/packages/php-mqtt/laravel-client)
[![Total Downloads](https://poser.pugx.org/php-mqtt/laravel-client/downloads)](https://packagist.org/packages/php-mqtt/laravel-client) [![Total Downloads](https://poser.pugx.org/php-mqtt/laravel-client/downloads)](https://packagist.org/packages/php-mqtt/laravel-client)
@ -16,10 +16,10 @@ allows you to connect to an MQTT broker where you can publish messages and subsc
## Installation ## Installation
The package is available on [packagist.org](https://packagist.org/packages/php-mqtt/laravel-client) and can be installed using composer: The package is available on [packagist.org](https://packagist.org/packages/ykxiao/laravel-mqtt-client) and can be installed using composer:
```bash ```bash
composer require php-mqtt/laravel-client composer require ykxiao/laravel-mqtt-client
``` ```
The package will register itself through Laravel auto discovery of packages. The package will register itself through Laravel auto discovery of packages.
@ -139,7 +139,7 @@ This library allows you to use all the features provided by [`php-mqtt/client`](
Simply retrieve an instance of `\PhpMqtt\Client\Contracts\MqttClient` with `MQTT::connection(string $name = null)` and use it directly. Simply retrieve an instance of `\PhpMqtt\Client\Contracts\MqttClient` with `MQTT::connection(string $name = null)` and use it directly.
For an extensive collection of examples which explain how to use the MQTT client (directly), For an extensive collection of examples which explain how to use the MQTT client (directly),
you can visit the [`php-mqtt/client-examples` repository](https://github.com/php-mqtt/client-examples). you can visit the [`php-mqtt/client-examples` repository](https://git.tool.dwoodauto.com/ykxiao/client-examples).
## License ## License

View File

@ -1,6 +1,6 @@
{ {
"name": "php-mqtt/laravel-client", "name": "ykxiao/laravel-mqtt-client",
"description": "A wrapper for the php-mqtt/client library for Laravel.", "description": "A wrapper for the ykxiao/laravel-mqtt-client library for Laravel.",
"type": "library", "type": "library",
"keywords": [ "keywords": [
"mqtt", "mqtt",
@ -9,7 +9,7 @@
"subscribe", "subscribe",
"laravel" "laravel"
], ],
"homepage": "https://github.com/php-mqtt/laravel-client", "homepage": "https://git.tool.dwoodauto.com/ykxiao/laravel-matt-client",
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
{ {
@ -20,9 +20,9 @@
], ],
"require": { "require": {
"php": "^7.4|^8.0", "php": "^7.4|^8.0",
"illuminate/config": "~7.0|~8.0", "illuminate/config": "^7.0|^8.0|^9.0|^10.0",
"illuminate/support": "~7.0|~8.0", "illuminate/support": "^7.0|^8.0|^9.0|^10.0",
"php-mqtt/client": "^1.0" "php-mqtt/client": "^1.3.0"
}, },
"require-dev": { "require-dev": {
"squizlabs/php_codesniffer": "^3.5" "squizlabs/php_codesniffer": "^3.5"

View File

@ -55,6 +55,10 @@ return [
// with the log level as configured. // with the log level as configured.
'enable_logging' => env('MQTT_ENABLE_LOGGING', true), 'enable_logging' => env('MQTT_ENABLE_LOGGING', true),
// Which logging channel to use for logs produced by the MQTT client.
// If left empty, the default log channel or stack is being used.
'log_channel' => env('MQTT_LOG_CHANNEL', null),
// Defines which repository implementation shall be used. Currently, // Defines which repository implementation shall be used. Currently,
// only a MemoryRepository is supported. // only a MemoryRepository is supported.
'repository' => MemoryRepository::class, 'repository' => MemoryRepository::class,
@ -101,6 +105,14 @@ return [
// The interval (in seconds) in which the client will send a ping to the broker, // The interval (in seconds) in which the client will send a ping to the broker,
// if no other message has been sent. // if no other message has been sent.
'keep_alive_interval' => env('MQTT_KEEP_ALIVE_INTERVAL', 10), 'keep_alive_interval' => env('MQTT_KEEP_ALIVE_INTERVAL', 10),
// Additional settings for the optional auto-reconnect. The delay between reconnect attempts is in seconds.
'auto_reconnect' => [
'enabled' => env('MQTT_AUTO_RECONNECT_ENABLED', false),
'max_reconnect_attempts' => env('MQTT_AUTO_RECONNECT_MAX_RECONNECT_ATTEMPTS', 3),
'delay_between_reconnect_attempts' => env('MQTT_AUTO_RECONNECT_DELAY_BETWEEN_RECONNECT_ATTEMPTS', 0),
],
], ],
], ],

View File

@ -60,6 +60,12 @@ class ConnectionManager
$name = $this->defaultConnection; $name = $this->defaultConnection;
} }
// Remove the connection if it is in a disconnected state.
// Doing this instead of simply reconnecting ensures the caller will get a fresh connection.
if (array_key_exists($name, $this->connections) && !$this->connections[$name]->isConnected()) {
unset($this->connections[$name]);
}
if (!array_key_exists($name, $this->connections)) { if (!array_key_exists($name, $this->connections)) {
$this->connections[$name] = $this->createConnection($name); $this->connections[$name] = $this->createConnection($name);
} }
@ -133,11 +139,16 @@ class ConnectionManager
$cleanSession = (bool) Arr::get($config, 'use_clean_session', true); $cleanSession = (bool) Arr::get($config, 'use_clean_session', true);
$repository = Arr::get($config, 'repository', Repository::class); $repository = Arr::get($config, 'repository', Repository::class);
$loggingEnabled = (bool) Arr::get($config, 'enable_logging', true); $loggingEnabled = (bool) Arr::get($config, 'enable_logging', true);
$logChannel = Arr::get($config, 'log_channel', null);
$settings = $this->buildConnectionSettings(Arr::get($config, 'connection_settings', [])); $settings = $this->buildConnectionSettings(Arr::get($config, 'connection_settings', []));
$repository = $this->application->make($repository); $repository = $this->application->make($repository);
$logger = $loggingEnabled ? $this->application->make('log') : null; $logger = $loggingEnabled ? $this->application->make('log') : null;
if ($logger && $logChannel) {
$logger = $logger->channel($logChannel);
}
$client = new MqttClient($host, $port, $clientId, $protocol, $repository, $logger); $client = new MqttClient($host, $port, $clientId, $protocol, $repository, $logger);
$client->connect($settings, $cleanSession); $client->connect($settings, $cleanSession);
@ -171,6 +182,9 @@ class ConnectionManager
->setLastWillTopic(Arr::get($config, 'last_will.topic')) ->setLastWillTopic(Arr::get($config, 'last_will.topic'))
->setLastWillMessage(Arr::get($config, 'last_will.message')) ->setLastWillMessage(Arr::get($config, 'last_will.message'))
->setLastWillQualityOfService((int) Arr::get($config, 'last_will.quality_of_service', MqttClient::QOS_AT_MOST_ONCE)) ->setLastWillQualityOfService((int) Arr::get($config, 'last_will.quality_of_service', MqttClient::QOS_AT_MOST_ONCE))
->setRetainLastWill((bool) Arr::get($config, 'last_will.retain', false)); ->setRetainLastWill((bool) Arr::get($config, 'last_will.retain', false))
->setReconnectAutomatically((bool) Arr::get($config, 'auto_reconnect.enabled', false))
->setMaxReconnectAttempts((int) Arr::get($config, 'auto_reconnect.max_reconnect_attempts', 3))
->setDelayBetweenReconnectAttempts((int) Arr::get($config, 'auto_reconnect.delay_between_reconnect_attempts', 0));
} }
} }