Skip to content

Commit d1a01fb

Browse files
authored
Support mqtt over websocket (#109)
1 parent 5cc7ef3 commit d1a01fb

24 files changed

+1429
-353
lines changed

Diff for: .github/workflows/phpstan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030

3131
- name: Setup Dependencies
3232
env:
33-
COMPOSER_ROOT_VERSION: 1.x-dev
33+
COMPOSER_ROOT_VERSION: 2.x-dev
3434
run:
3535
composer install -o
3636

Diff for: .github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jobs:
8080
8181
- name: Setup Dependencies
8282
env:
83-
COMPOSER_ROOT_VERSION: 1.x-dev
83+
COMPOSER_ROOT_VERSION: 2.x-dev
8484
run: composer install -o
8585

8686
- name: Coding Standards Check

Diff for: README-CN.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
支持 MQTT 协议 `3.1``3.1.1``5.0` 版本,支持`QoS 0``QoS 1``QoS 2`
88

9+
支持 MQTT over WebSocket。
10+
911
> 首个支持 MQTT `5.0` 协议的 PHP library。
1012
1113
[![License](https://poser.pugx.org/simps/mqtt/license)](LICENSE)

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Support for MQTT protocol versions `3.1`, `3.1.1` and `5.0`.
88

99
Support for `QoS 0`, `QoS 1`, `QoS 2`.
1010

11+
Support for MQTT over WebSocket.
12+
1113
> The first PHP library to support the MQTT `5.0` protocol.
1214
1315
[![License](https://poser.pugx.org/simps/mqtt/license)](LICENSE)

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
},
5656
"extra": {
5757
"branch-alias": {
58-
"dev-master": "1.x-dev"
58+
"dev-master": "2.x-dev"
5959
}
6060
}
6161
}

Diff for: docs/_coverpage.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
- Support for QoS 0, 1, 2
88
- Support for MQTT 3.1, 3.1.1, 5.0
9+
- Support for MQTT over WebSocket
910
- The first PHP library to support the MQTT 5.0 protocol
1011

1112
[Gitee](https://gitee.com/phpmqtt/mqtt)

Diff for: docs/en/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Support for MQTT protocol versions `3.1`, `3.1.1` and `5.0`.
66

77
Support for `QoS 0`, `QoS 1`, `QoS 2`.
88

9+
Support for MQTT over WebSocket.
10+
911
## Requirements
1012

1113
* PHP >= `7.1`

Diff for: docs/en/_sidebar.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
* MQTT Coroutine Client
2-
* [Client API](en/client)
1+
- MQTT Coroutine Client
2+
- [Client API](en/client)
3+
- [WebSocket Client API](en/websocket)
34

4-
* MQTT Protocol Analysis
5-
* [Protocol API](en/protocol)
5+
- MQTT Protocol Analysis
6+
- [Protocol API](en/protocol)
67

7-
* MQTT Message
8-
* [Message API](en/message)
8+
- MQTT Message
9+
- [Message API](en/message)
910

10-
* Upgrade Guide
11-
* [1.2 Upgrade Guide](en/upgrade/1.2.md)
11+
- Upgrade Guide
12+
- [1.2 Upgrade Guide](en/upgrade/1.2)
13+
14+
- Tools
15+
- [Debug Tools](en/debug)

Diff for: docs/en/client.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Create a MQTT client instance
88
Simps\MQTT\Client::__construct(string $host, int $port, ClientConfig $config, int $clientType = Client::COROUTINE_CLIENT_TYPE)
99
```
1010

11-
* `string $host`
11+
- `string $host`
1212

1313
Broker's host
1414

15-
* `int $port`
15+
- `int $port`
1616

1717
Broker's port
1818

19-
* `ClientConfig $config`
19+
- `ClientConfig $config`
2020

2121
ClientConfig object.
2222

@@ -39,9 +39,9 @@ $configObj = new Simps\MQTT\Config\ClientConfig($config);
3939
$client = new Simps\MQTT\Client('127.0.0.1', 1883, $configObj);
4040
```
4141

42-
!> The Client will use the corresponding protocol resolution according to the `protocol_level` set.
42+
!> The Client will use the corresponding protocol resolution according to the `protocolLevel` set.
4343

44-
* `int $clientType`
44+
- `int $clientType`
4545

4646
Set the client type, use a Coroutine Client or a Sync Client, the default is Coroutine Client.
4747

@@ -55,13 +55,13 @@ Connect Broker
5555
Simps\MQTT\Client->connect(bool $clean = true, array $will = [])
5656
```
5757

58-
* `bool $clean`
58+
- `bool $clean`
5959

6060
Clean session. default is `true`.
6161

6262
For a detailed description, please see the corresponding protocol document: `Clean Session`.
6363

64-
* `array $will`
64+
- `array $will`
6565

6666
When a client is disconnected, Broker will automatically send a will message to other clients
6767

@@ -91,13 +91,13 @@ Subscribe to one topic or multiple topics
9191
Simps\MQTT\Client->subscribe(array $topics, array $properties = [])
9292
```
9393

94-
* `array $topics`
94+
- `array $topics`
9595

9696
```php
9797
// MQTT 3.x
9898
$topics = [
9999
// topic => Qos
100-
'topic1' => 0,
100+
'topic1' => 0,
101101
'topic2' => 1,
102102
];
103103

@@ -109,17 +109,17 @@ $topics = [
109109
'no_local' => true,
110110
'retain_as_published' => true,
111111
'retain_handling' => 2,
112-
],
112+
],
113113
'topic2' => [
114114
'qos' => 2,
115115
'no_local' => false,
116116
'retain_as_published' => true,
117117
'retain_handling' => 1,
118-
],
118+
],
119119
];
120120
```
121121

122-
* `array $properties`
122+
- `array $properties`
123123

124124
Optional in MQTT5
125125

@@ -131,13 +131,13 @@ Unsubscribe from a topic or multiple topics
131131
Simps\MQTT\Client->unSubscribe(array $topics, array $properties = [])
132132
```
133133

134-
* `array $topics`
134+
- `array $topics`
135135

136136
```php
137137
$topics = ['topic1', 'topic2'];
138138
```
139139

140-
* `array $properties`
140+
- `array $properties`
141141

142142
Optional in MQTT5
143143

@@ -157,14 +157,6 @@ New AUTH type added in MQTT5. Authentication exchange.
157157
Simps\MQTT\Client->auth(int $code = ReasonCode::SUCCESS, array $properties = [])
158158
```
159159

160-
## recv()
161-
162-
Receive messages
163-
164-
```php
165-
Simps\MQTT\Client->recv(): bool|arary|string
166-
```
167-
168160
## send()
169161

170162
Send messages
@@ -173,14 +165,22 @@ Send messages
173165
Simps\MQTT\Client->send(array $data, $response = true)
174166
```
175167

176-
* `array $data`
168+
- `array $data`
177169

178170
`$data` is the data to be sent and must contain information such as `type`
179171

180-
* `bool $response`
172+
- `bool $response`
181173

182174
Are acknowledgements required. If `true`, `recv()` is called once
183175

176+
## recv()
177+
178+
Receive messages
179+
180+
```php
181+
Simps\MQTT\Client->recv(): bool|arary|string
182+
```
183+
184184
## ping()
185185

186186
Send a heartbeat

Diff for: docs/en/debug.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Debug Tools
2+
3+
The tool provides 5 methods for debugging binary data, essentially functioning as a binary data viewer.
4+
5+
It primarily converts binary data into ASCII or hexadecimal formats for viewing, useful for debugging TCP, WebSocket, UDP, and other protocols.
6+
7+
```php
8+
public function hexDump(): string // Display in hexadecimal
9+
public function hexDumpAscii(): string // Display in both hexadecimal and corresponding ASCII characters
10+
public function printableText(): string // Printable characters
11+
public function hexStream(): string // Hexadecimal stream
12+
public function ascii(): string // Display in ASCII characters
13+
```
14+
15+
You can call these methods statically or instantiate `Simps\MQTT\Tools\Debug` or `Simps\MQTT\Tools\Common`/`Simps\MQTT\Tools\UnPackTool`:
16+
17+
- Instantiation
18+
19+
```php
20+
use Simps\MQTT\Tools\Debug;
21+
22+
$debug = new Debug('0:simps-mqtt/user001/update{
23+
"msg": "hello, mqtt"
24+
}');
25+
26+
//$debug = (new Debug())->setEncode('0:simps-mqtt/user001/update{
27+
// "msg": "hello, mqtt"
28+
//}');
29+
30+
echo $debug->hexDump(), PHP_EOL;
31+
echo $debug->hexDumpAscii(), PHP_EOL;
32+
echo $debug->printableText(), PHP_EOL;
33+
echo $debug->hexStream(), PHP_EOL;
34+
echo $debug->ascii();
35+
```
36+
37+
- Static call
38+
39+
```php
40+
use Simps\MQTT\Tools\UnPackTool;
41+
42+
echo UnPackTool::hexDumpAscii('0:simps-mqtt/user001/update{
43+
"msg": "hello, mqtt"
44+
}');
45+
```
46+
47+
```text
48+
00000000 30 3a 73 69 6d 70 73 2d 6d 71 74 74 2f 75 73 65 0:simps-mqtt/use
49+
00000010 72 30 30 31 2f 75 70 64 61 74 65 7b 0a 20 20 22 r001/update{. "
50+
00000020 6d 73 67 22 3a 20 22 68 65 6c 6c 6f 2c 20 6d 71 msg": "hello, mq
51+
00000030 74 74 22 0a 7d tt".}
52+
```

0 commit comments

Comments
 (0)