Skip to content

Commit 5c33d44

Browse files
bingcool黄增冰sy-records
authored
Support publish in fpm (#13)
Co-authored-by: 黄增冰 <[email protected]> Co-authored-by: sy-records <[email protected]>
1 parent d281d5f commit 5c33d44

File tree

8 files changed

+264
-75
lines changed

8 files changed

+264
-75
lines changed

composer.json

+37-37
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
{
2-
"name": "simps/mqtt",
3-
"description": "MQTT Protocol Analysis and Coroutine Client for PHP",
4-
"keywords": [
5-
"php",
6-
"simps",
7-
"mqtt",
8-
"swoole",
9-
"client",
10-
"coroutine",
11-
"mqtt_client",
12-
"mqtt_protocol",
13-
"mqtt3.1",
14-
"mqtt5.0"
15-
],
16-
"license": "Apache-2.0",
17-
"authors": [
18-
{
19-
"name": "Lu Fei",
20-
"email": "[email protected]"
21-
}
22-
],
23-
"require": {
24-
"php": ">=7.0",
25-
"ext-mbstring": "*",
26-
"ext-swoole": ">=4.4.19"
27-
},
28-
"require-dev": {
29-
"friendsofphp/php-cs-fixer": "^2.16"
30-
},
31-
"autoload": {
32-
"psr-4": {
33-
"Simps\\MQTT\\": "src/"
34-
}
35-
},
36-
"scripts": {
37-
"cs-check": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix --dry-run",
38-
"cs-fix": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix"
2+
"name": "simps/mqtt",
3+
"description": "MQTT Protocol Analysis and Coroutine Client for PHP",
4+
"keywords": [
5+
"php",
6+
"simps",
7+
"mqtt",
8+
"swoole",
9+
"client",
10+
"coroutine",
11+
"mqtt_client",
12+
"mqtt_protocol",
13+
"mqtt3.1",
14+
"mqtt5.0"
15+
],
16+
"license": "Apache-2.0",
17+
"authors": [
18+
{
19+
"name": "Lu Fei",
20+
"email": "[email protected]"
3921
}
22+
],
23+
"require": {
24+
"php": ">=7.0",
25+
"ext-mbstring": "*",
26+
"ext-swoole": ">=4.4.19"
27+
},
28+
"require-dev": {
29+
"friendsofphp/php-cs-fixer": "^2.16"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"Simps\\MQTT\\": "src/"
34+
}
35+
},
36+
"scripts": {
37+
"cs-check": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix --dry-run",
38+
"cs-fix": "/usr/bin/env php ./vendor/bin/php-cs-fixer fix"
39+
}
4040
}

docs/en/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ Create a MQTT client instance
3030
Simps\MQTT\Client::__construct(array $config, array $swConfig = [], int $type = SWOOLE_SOCK_TCP)
3131
```
3232

33+
Create a MQTT client instance of Fpm|Apache, mainly used for publish messages, the fourth parameter of clientType = \Simps\MQTT\Client::SYNC_CLIENT_TYPE
34+
35+
```php
36+
Simps\MQTT\Client::__construct(array $config, array $swConfig = [], int $type = SWOOLE_SOCK_TCP, int clientType = \Simps\MQTT\Client::SYNC_CLIENT_TYPE)
37+
```
38+
3339
* `array $config`
3440

3541
An array of client options, you can set the following options:

docs/zh-cn/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ composer require simps/mqtt
3030
Simps\MQTT\Client::__construct(array $config, array $swConfig = [], int $type = SWOOLE_SOCK_TCP)
3131
```
3232

33+
创建一个适用于Fpm|Apache环境的MQTT客户端实例,主要用于publish消息,设置第四个参数clientType = \Simps\MQTT\Client::SYNC_CLIENT_TYPE
34+
```php
35+
Simps\MQTT\Client::__construct(array $config, array $swConfig = [], int $type = SWOOLE_SOCK_TCP, int clientType = \Simps\MQTT\Client::SYNC_CLIENT_TYPE)
36+
```
37+
3338
* 参数`array $config`
3439

3540
客户端选项数组,可以设置以下选项:

examples/publish_fpm.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* This file is part of Simps
4+
*
5+
* @link https://github.com/simps/mqtt
6+
* @contact Lu Fei <[email protected]>
7+
*
8+
* For the full copyright and license information,
9+
* please view the LICENSE file that was distributed with this source code
10+
*/
11+
12+
include __DIR__ . '/../vendor/autoload.php';
13+
14+
use Swoole\Coroutine;
15+
use Simps\MQTT\Client;
16+
17+
/**
18+
* 适用于fpm环境下发布信息,指定第四个参数clientType = Client::SYNC_CLIENT_TYPE
19+
*/
20+
21+
$config = [
22+
'host' => '127.0.0.1',
23+
'port' => 1883,
24+
'time_out' => 5,
25+
'user_name' => 'user001',
26+
'password' => 'hLXQ9ubnZGzkzf',
27+
'client_id' => Client::genClientID(),
28+
'keep_alive' => 20,
29+
];
30+
31+
$client = new Client(
32+
$config,
33+
['open_mqtt_protocol' => true, 'package_max_length' => 2 * 1024 * 1024],
34+
SWOOLE_SOCK_TCP,
35+
Client::SYNC_CLIENT_TYPE
36+
);
37+
while (!$client->connect()) {
38+
sleep(3);
39+
$client->connect();
40+
}
41+
while (true) {
42+
$response = $client->publish(
43+
'simps-mqtt/user001/update',
44+
'{"time":' . time() . '}',
45+
1,
46+
0,
47+
0,
48+
[
49+
'topic_alias' => 1,
50+
'message_expiry_interval' => 12
51+
]
52+
);
53+
var_dump($response);
54+
sleep(3);
55+
}

examples/server.php

+19-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
try {
3434
Common::printf($data);
3535
$data = Protocol::unpack($data);
36-
var_dump($data);
3736
if (is_array($data) && isset($data['type'])) {
3837
switch ($data['type']) {
3938
case Types::CONNECT:
@@ -66,20 +65,25 @@
6665
break;
6766
case Types::PUBLISH:
6867
// Send to subscribers
69-
$server->send(
70-
1,
71-
Protocol::pack(
72-
[
73-
'type' => $data['type'],
74-
'topic' => $data['topic'],
75-
'message' => $data['message'],
76-
'dup' => $data['dup'],
77-
'qos' => $data['qos'],
78-
'retain' => $data['retain'],
79-
'message_id' => $data['message_id'] ?? ''
80-
]
81-
)
82-
);
68+
foreach ($server->connections as $sub_fd) {
69+
if($sub_fd != $fd) {
70+
$server->send(
71+
$sub_fd,
72+
Protocol::pack(
73+
[
74+
'type' => $data['type'],
75+
'topic' => $data['topic'],
76+
'message' => $data['message'],
77+
'dup' => $data['dup'],
78+
'qos' => $data['qos'],
79+
'retain' => $data['retain'],
80+
'message_id' => $data['message_id'] ?? ''
81+
]
82+
)
83+
);
84+
}
85+
}
86+
8387

8488
if ($data['qos'] === 1) {
8589
$server->send(

examples/v5/publish_fpm.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* This file is part of Simps
4+
*
5+
* @link https://github.com/simps/mqtt
6+
* @contact Lu Fei <[email protected]>
7+
*
8+
* For the full copyright and license information,
9+
* please view the LICENSE file that was distributed with this source code
10+
*/
11+
12+
include __DIR__ . '/../../vendor/autoload.php';
13+
14+
use Swoole\Coroutine;
15+
use Simps\MQTT\Client;
16+
17+
/**
18+
* 此进程适用于fpm环境下发布信息,指定第四个参数Client::SYNC_CLIENT_TYPE
19+
*/
20+
21+
$config = [
22+
'host' => '127.0.0.1',
23+
// 'host' => 'broker.emqx.io',
24+
'port' => 1883,
25+
'time_out' => 5,
26+
'user_name' => 'user001',
27+
'password' => 'hLXQ9ubnZGzkzf',
28+
'client_id' => Client::genClientID(),
29+
'keep_alive' => 20,
30+
'properties' => [
31+
'session_expiry_interval' => 213,
32+
'receive_maximum' => 221,
33+
'topic_alias_maximum' => 313,
34+
],
35+
'protocol_level' => 5,
36+
];
37+
38+
$client = new Client(
39+
$config,
40+
['open_mqtt_protocol' => true, 'package_max_length' => 2 * 1024 * 1024],
41+
SWOOLE_SOCK_TCP,
42+
Client::SYNC_CLIENT_TYPE
43+
);
44+
while (!$client->connect()) {
45+
sleep(3);
46+
$client->connect();
47+
}
48+
while (true) {
49+
$response = $client->publish(
50+
'simps-mqtt/user001/update',
51+
'{"time":' . time() . '}',
52+
1,
53+
0,
54+
0,
55+
[
56+
'topic_alias' => 1,
57+
'message_expiry_interval' => 12
58+
]
59+
);
60+
var_dump($response);
61+
sleep(3);
62+
}

examples/v5/server.php

+18-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
try {
3434
Common::printf($data);
3535
$data = ProtocolV5::unpack($data);
36-
var_dump($data);
3736
if (is_array($data) && isset($data['type'])) {
3837
switch ($data['type']) {
3938
case Types::CONNECT:
@@ -74,20 +73,24 @@
7473
break;
7574
case Types::PUBLISH:
7675
// Send to subscribers
77-
$server->send(
78-
1,
79-
ProtocolV5::pack(
80-
[
81-
'type' => $data['type'],
82-
'topic' => $data['topic'],
83-
'message' => $data['message'],
84-
'dup' => $data['dup'],
85-
'qos' => $data['qos'],
86-
'retain' => $data['retain'],
87-
'message_id' => $data['message_id'] ?? ''
88-
]
89-
)
90-
);
76+
foreach($server->connections as $sub_fd) {
77+
if($sub_fd != $fd) {
78+
$server->send(
79+
$sub_fd,
80+
ProtocolV5::pack(
81+
[
82+
'type' => $data['type'],
83+
'topic' => $data['topic'],
84+
'message' => $data['message'],
85+
'dup' => $data['dup'],
86+
'qos' => $data['qos'],
87+
'retain' => $data['retain'],
88+
'message_id' => $data['message_id'] ?? ''
89+
]
90+
)
91+
);
92+
}
93+
}
9194

9295
if ($data['qos'] === 1) {
9396
$server->send(

0 commit comments

Comments
 (0)