Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions .github/ISSUE_TEMPLATE/---bug-report.md

This file was deleted.

10 changes: 0 additions & 10 deletions .github/ISSUE_TEMPLATE/---support-question.md

This file was deleted.

46 changes: 46 additions & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: "🐛 Bug report"
description: Create a report to help us improve
labels:
- bug
assignees:
- sy-records
body:
- type: textarea
id: description
attributes:
label: Description
description: Describe the bug and expected behavior.
validations:
required: true

- type: textarea
id: environment
attributes:
label: Environment
description: |
Execute the command and paste the result below.

Command: `php -v && php --ri swoole`
render: bash
validations:
required: true

- type: textarea
id: reproducible-code
attributes:
label: Reproducible codes
description: Provide the minimum code needed to reproduce the issue.
placeholder: "# Paste your code here."
render: php
validations:
required: true

- type: textarea
id: packet-capture
attributes:
label: Optional packet capture logs
description: |
You can also provide tcpdump packet capture logs.
Example command: `tcpdump -i en0 port 1883 -w mqtt.pcap`
Please change the network card and port to match your environment, then attach `mqtt.pcap`.

4 changes: 0 additions & 4 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
blank_issues_enabled: false
contact_links:
- name: Simps - the community chat
url: https://discord.gg/u4YAqeh
about: Join Simps community and chat about Simps, Swoole, MQTT...
21 changes: 21 additions & 0 deletions .github/ISSUE_TEMPLATE/support-question.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: "🧐 Support Question"
description: Ask for help
labels:
- question
assignees:
- sy-records
body:
- type: textarea
id: question
attributes:
label: Question
description: Describe the problem or question you need help with.
validations:
required: true

- type: textarea
id: context
attributes:
label: Context
description: Add relevant code snippets, logs, or environment details.

2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: [ '8.1', '8.2', '8.3', '8.4' ]
php-version: [ '8.3', '8.4', '8.5' ]
max-parallel: 5
fail-fast: false
steps:
Expand Down
10 changes: 4 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest]
php-version: [ '8.1', '8.2', '8.3', '8.4', '8.5' ]
swoole-version: [ 'v4.8.13', 'v5.1.8', 'v6.1.6', 'master' ]
swoole-version: [ 'v4.8.13', 'v5.1.8-end', 'v6.2.0', 'master' ]
exclude:
- php-version: '8.5'
swoole-version: 'v6.1.6'
- php-version: '8.5'
swoole-version: 'v5.1.8'
swoole-version: 'v5.1.8-end'
- php-version: '8.5'
swoole-version: 'v4.8.13'
- php-version: '8.4'
swoole-version: 'v5.1.8'
swoole-version: 'v5.1.8-end'
- php-version: '8.4'
swoole-version: 'v4.8.13'
- php-version: '8.3'
swoole-version: 'v4.8.13'
- php-version: '8.1'
swoole-version: 'v6.1.5'
swoole-version: 'v6.2.0'
- php-version: '8.1'
swoole-version: 'master'
max-parallel: 10
Expand Down
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[![PHP Version](https://img.shields.io/badge/php-%3E=7.1-blue.svg)](https://www.php.net)
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.20-blue.svg)](https://github.com/swoole/swoole-src)

[![GitHub Actions](https://github.com/simps/mqtt/workflows/PHPUnit%20for%20MQTT/badge.svg)](https://github.com/simps/mqtt/actions)
[![PHPUnit for MQTT](https://github.com/simps/mqtt/actions/workflows/test.yml/badge.svg)](https://github.com/simps/mqtt/actions/workflows/test.yml)
[![Static Code Analysis](https://github.com/simps/mqtt/actions/workflows/phpstan.yml/badge.svg)](https://github.com/simps/mqtt/actions/workflows/phpstan.yml)
[![Gitee Star](https://gitee.com/phpmqtt/mqtt/badge/star.svg?theme=dark)](https://gitee.com/phpmqtt/mqtt/stargazers)
[![GitCode Star](https://gitcode.com/simps/mqtt/star/badge.svg)](https://gitcode.com/simps/mqtt/stargazers)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Support for MQTT over WebSocket.
[![PHP Version](https://img.shields.io/badge/php-%3E=7.1-blue.svg)](https://www.php.net)
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.20-blue.svg)](https://github.com/swoole/swoole-src)

[![GitHub Actions](https://github.com/simps/mqtt/workflows/PHPUnit%20for%20MQTT/badge.svg)](https://github.com/simps/mqtt/actions)
[![PHPUnit for MQTT](https://github.com/simps/mqtt/actions/workflows/test.yml/badge.svg)](https://github.com/simps/mqtt/actions/workflows/test.yml)
[![Static Code Analysis](https://github.com/simps/mqtt/actions/workflows/phpstan.yml/badge.svg)](https://github.com/simps/mqtt/actions/workflows/phpstan.yml)
[![Gitee Star](https://gitee.com/phpmqtt/mqtt/badge/star.svg?theme=dark)](https://gitee.com/phpmqtt/mqtt/stargazers)
[![GitCode Star](https://gitcode.com/simps/mqtt/star/badge.svg)](https://gitcode.com/simps/mqtt/stargazers)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"phpunit/phpunit": "^8.5",
"swoole/ide-helper": ">=4.4.20",
"simps/mqtt-cli": "*",
"phpstan/phpstan": "^1.0"
"phpstan/phpstan": "^2.0"
},
"suggest": {
"ext-swoole": "The ext-swoole >= v4.4.20 or v4.5.3 needs to be loaded when using the MQTT Client."
Expand Down
7 changes: 4 additions & 3 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
const SWOOLE_MQTT_CONFIG = [
'open_mqtt_protocol' => true,
'package_max_length' => 2 * 1024 * 1024,
'connect_timeout' => 5.0,
'write_timeout' => 5.0,
'read_timeout' => 5.0,
'connect_timeout' => 10.0,
'write_timeout' => 10.0,
'read_timeout' => 10.0,
];

const SIMPS_MQTT_LOCAL_HOST = '127.0.0.1';
const SIMPS_MQTT_REMOTE_HOST = 'broker.emqx.io';
const SIMPS_MQTT_MOSQUITTO = 'test.mosquitto.org';
const SIMPS_MQTT_PORT = 1883;
const SIMPS_MQTT_OVER_WEBSOCKET_PORT = 8083;

Expand Down
2 changes: 1 addition & 1 deletion examples/ssl_ca.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
->setSwooleConfig($swooleConfig)
->setSockType(SWOOLE_SOCK_TCP | SWOOLE_SSL);

$client = new Client('test.mosquitto.org', 8883, $config);
$client = new Client(SIMPS_MQTT_MOSQUITTO, 8883, $config);
$client->connect();
$topics['testtopic/#'] = 0;
$client->subscribe($topics);
Expand Down
2 changes: 1 addition & 1 deletion examples/ssl_ca_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
->setSwooleConfig($swooleConfig)
->setSockType(SWOOLE_SOCK_TCP | SWOOLE_SSL);

$client = new Client('test.mosquitto.org', 8884, $config);
$client = new Client(SIMPS_MQTT_MOSQUITTO, 8884, $config);
$client->connect();
$topics['testtopic/#'] = 0;
$client->subscribe($topics);
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon

parameters:
level: 6
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
bootstrapFiles:
- examples/bootstrap.php
paths:
Expand Down
39 changes: 37 additions & 2 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
use Swoole\Coroutine;
use Swoole\Coroutine\Http\Client as WebSocketClient;

/**
* @phpstan-import-type ConnectData from \Simps\MQTT\PhpStanTypes
* @phpstan-import-type MqttProperties from \Simps\MQTT\PhpStanTypes
* @phpstan-import-type PacketData from \Simps\MQTT\PhpStanTypes
* @phpstan-import-type SubscribeTopics from \Simps\MQTT\PhpStanTypes
* @phpstan-import-type UnsubscribeTopics from \Simps\MQTT\PhpStanTypes
* @phpstan-import-type WillData from \Simps\MQTT\PhpStanTypes
*/
abstract class BaseClient
{
public const COROUTINE_CLIENT_TYPE = 1;
Expand All @@ -34,7 +42,7 @@ abstract class BaseClient
/** @var int */
private $messageId = 0;

/** @var array */
/** @var ConnectData */
private $connectData = [];

/** @var string */
Expand Down Expand Up @@ -155,6 +163,10 @@ public function getSsl(): bool
return $this->ssl;
}

/**
* @param ConnectData $connectData
* @return $this
*/
public function setConnectData(array $connectData): self
{
$this->connectData = $connectData;
Expand All @@ -163,7 +175,7 @@ public function setConnectData(array $connectData): self
}

/**
* @return null|array|string
* @return ($key is null ? ConnectData : mixed|null)
*/
public function getConnectData(?string $key = null)
{
Expand Down Expand Up @@ -238,6 +250,9 @@ protected function handleException(): void
throw new ConnectException($errMsg, $this->client->errCode);
}

/**
* @param array{}|WillData $will
*/
public function connect(bool $clean = true, array $will = [])
{
$data = [
Expand All @@ -263,6 +278,10 @@ public function connect(bool $clean = true, array $will = [])
return $this->send($data);
}

/**
* @param SubscribeTopics $topics
* @param MqttProperties $properties
*/
public function subscribe(array $topics, array $properties = [])
{
return $this->send([
Expand All @@ -273,6 +292,10 @@ public function subscribe(array $topics, array $properties = [])
]);
}

/**
* @param UnsubscribeTopics $topics
* @param MqttProperties $properties
*/
public function unSubscribe(array $topics, array $properties = [])
{
return $this->send([
Expand All @@ -283,6 +306,9 @@ public function unSubscribe(array $topics, array $properties = [])
]);
}

/**
* @param MqttProperties $properties
*/
public function publish(
string $topic,
string $message,
Expand Down Expand Up @@ -329,20 +355,29 @@ public function ping(bool $response = true)
return $this->send(['type' => Protocol\Types::PINGREQ], $response);
}

/**
* @param MqttProperties $properties
*/
public function close(int $code = ReasonCode::NORMAL_DISCONNECTION, array $properties = []): bool
{
$this->send(['type' => Protocol\Types::DISCONNECT, 'code' => $code, 'properties' => $properties], false);

return $this->client->close();
}

/**
* @param MqttProperties $properties
*/
public function auth(int $code = ReasonCode::SUCCESS, array $properties = [])
{
return $this->send(['type' => Protocol\Types::AUTH, 'code' => $code, 'properties' => $properties]);
}

abstract protected function reConnect(): void;

/**
* @param PacketData $data
*/
abstract public function send(array $data, bool $response = true);

abstract public function recv();
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function recv()
$this->connect($this->getConnectData('clean_session') ?? true, $this->getConnectData('will') ?? []);
} elseif ($response === false && $this->getClient()->errCode !== SOCKET_ETIMEDOUT) {
$this->handleException();
} elseif (is_string($response) && strlen($response) !== 0) {
} elseif (is_string($response)) {
$this->handleVerbose($response);

return $this->getConfig()->isMQTT5() ? Protocol\V5::unpack($response) : Protocol\V3::unpack($response);
Expand Down
6 changes: 6 additions & 0 deletions src/Config/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
*/
namespace Simps\MQTT\Config;

/**
* @phpstan-import-type ArrayMap from \Simps\MQTT\PhpStanTypes
*/
abstract class AbstractConfig
{
/**
* @param ArrayMap $data
*/
public function __construct(array $data = [])
{
foreach ($data as $k => $v) {
Expand Down
Loading
Loading