Skip to content

Commit 6eb522a

Browse files
committed
Update examples code
1 parent 5c33d44 commit 6eb522a

17 files changed

+229
-323
lines changed

examples/bootstrap.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+
foreach (
13+
[
14+
__DIR__ . '/../vendor/autoload.php',
15+
__DIR__ . '/vendor/autoload.php',
16+
__DIR__ . '/../../../autoload.php',
17+
] as $file
18+
) {
19+
if (file_exists($file)) {
20+
require $file;
21+
break;
22+
}
23+
}
24+
25+
const SSL_CERTS_DIR = __DIR__ . '/ssl_certs';
26+
27+
const SWOOLE_MQTT_CONFIG = [
28+
'open_mqtt_protocol' => true,
29+
'package_max_length' => 2 * 1024 * 1024,
30+
];
31+
32+
function getTestConnectConfig(string $host = '127.0.0.1')
33+
{
34+
return [
35+
'host' => $host,
36+
'port' => 1883,
37+
'time_out' => 5,
38+
'user_name' => 'username',
39+
'password' => 'password',
40+
'client_id' => \Simps\MQTT\Client::genClientID(),
41+
'keep_alive' => 20,
42+
];
43+
}
44+
45+
function getTestMQTT5ConnectConfig(string $host = '127.0.0.1')
46+
{
47+
return [
48+
'host' => $host,
49+
'port' => 1883,
50+
'time_out' => 5,
51+
'user_name' => 'username',
52+
'password' => 'password',
53+
'client_id' => \Simps\MQTT\Client::genClientID(),
54+
'keep_alive' => 20,
55+
'properties' => [
56+
'session_expiry_interval' => 60,
57+
'receive_maximum' => 65535,
58+
'topic_alias_maximum' => 65535,
59+
],
60+
'protocol_level' => 5,
61+
];
62+
}

examples/publish.php

+6-16
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,19 @@
99
* please view the LICENSE file that was distributed with this source code
1010
*/
1111

12-
include __DIR__ . '/../vendor/autoload.php';
12+
include __DIR__ . '/bootstrap.php';
1313

14-
use Swoole\Coroutine;
1514
use Simps\MQTT\Client;
15+
use Swoole\Coroutine;
1616

17-
$config = [
18-
'host' => '127.0.0.1',
19-
'port' => 1883,
20-
'time_out' => 5,
21-
'user_name' => 'user001',
22-
'password' => 'hLXQ9ubnZGzkzf',
23-
'client_id' => Client::genClientID(),
24-
'keep_alive' => 20,
25-
];
26-
27-
Coroutine\run(function () use ($config) {
28-
$client = new Client($config, ['open_mqtt_protocol' => true, 'package_max_length' => 2 * 1024 * 1024]);
29-
while (! $client->connect()) {
17+
Coroutine\run(function () {
18+
$client = new Client(getTestConnectConfig(), SWOOLE_MQTT_CONFIG);
19+
while (!$client->connect()) {
3020
Coroutine::sleep(3);
3121
$client->connect();
3222
}
3323
while (true) {
34-
$response = $client->publish('simps-mqtt/user001/update', '{"time":'. time() .'}', 1);
24+
$response = $client->publish('simps-mqtt/user001/update', '{"time":' . time() . '}', 1);
3525
var_dump($response);
3626
Coroutine::sleep(3);
3727
}

examples/publish_fpm.php

+5-30
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,22 @@
99
* please view the LICENSE file that was distributed with this source code
1010
*/
1111

12-
include __DIR__ . '/../vendor/autoload.php';
12+
include __DIR__ . '/bootstrap.php';
1313

14-
use Swoole\Coroutine;
1514
use Simps\MQTT\Client;
1615

1716
/**
18-
* 适用于fpm环境下发布信息,指定第四个参数clientType = Client::SYNC_CLIENT_TYPE
17+
* Support publish in fpm, need to specify clientType as Client::SYNC_CLIENT_TYPE
1918
*/
19+
$client = new Client(getTestConnectConfig(), SWOOLE_MQTT_CONFIG, SWOOLE_SOCK_TCP, Client::SYNC_CLIENT_TYPE);
2020

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-
);
3721
while (!$client->connect()) {
3822
sleep(3);
3923
$client->connect();
4024
}
25+
4126
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-
);
27+
$response = $client->publish('simps-mqtt/user001/update', '{"time":' . time() . '}', 1);
5328
var_dump($response);
5429
sleep(3);
5530
}

examples/server.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
* please view the LICENSE file that was distributed with this source code
1010
*/
1111

12-
include __DIR__ . '/../vendor/autoload.php';
12+
include __DIR__ . '/bootstrap.php';
1313

1414
use Simps\MQTT\Protocol;
15-
use Simps\MQTT\Types;
1615
use Simps\MQTT\Tools\Common;
16+
use Simps\MQTT\Types;
1717

1818
$server = new Swoole\Server('127.0.0.1', 1883, SWOOLE_BASE);
1919

2020
$server->set(
2121
[
2222
'open_mqtt_protocol' => true,
2323
'worker_num' => 2,
24-
'package_max_length' => 2 * 1024 * 1024
24+
'package_max_length' => 2 * 1024 * 1024,
2525
]
2626
);
2727

@@ -31,14 +31,16 @@
3131

3232
$server->on('receive', function (Swoole\Server $server, $fd, $from_id, $data) {
3333
try {
34-
Common::printf($data);
34+
// debug
35+
// Common::printf($data);
3536
$data = Protocol::unpack($data);
3637
if (is_array($data) && isset($data['type'])) {
3738
switch ($data['type']) {
3839
case Types::CONNECT:
3940
// Check protocol_name
4041
if ($data['protocol_name'] != 'MQTT') {
4142
$server->close($fd);
43+
4244
return false;
4345
}
4446

@@ -66,7 +68,7 @@
6668
case Types::PUBLISH:
6769
// Send to subscribers
6870
foreach ($server->connections as $sub_fd) {
69-
if($sub_fd != $fd) {
71+
if ($sub_fd != $fd) {
7072
$server->send(
7173
$sub_fd,
7274
Protocol::pack(
@@ -77,14 +79,13 @@
7779
'dup' => $data['dup'],
7880
'qos' => $data['qos'],
7981
'retain' => $data['retain'],
80-
'message_id' => $data['message_id'] ?? ''
82+
'message_id' => $data['message_id'] ?? '',
8183
]
8284
)
8385
);
8486
}
8587
}
8688

87-
8889
if ($data['qos'] === 1) {
8990
$server->send(
9091
$fd,
@@ -113,7 +114,7 @@
113114
[
114115
'type' => Types::SUBACK,
115116
'message_id' => $data['message_id'] ?? '',
116-
'payload' => $payload
117+
'payload' => $payload,
117118
]
118119
)
119120
);

examples/ssl_ca.php

+42-49
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,54 @@
99
* please view the LICENSE file that was distributed with this source code
1010
*/
1111

12-
include __DIR__ . '/../vendor/autoload.php';
12+
include __DIR__ . '/bootstrap.php';
1313

14-
use Swoole\Coroutine;
1514
use Simps\MQTT\Client;
15+
use Swoole\Coroutine;
16+
17+
Coroutine\run(function () {
18+
$config = [
19+
'host' => 'test.mosquitto.org',
20+
'port' => 8883,
21+
'time_out' => 5,
22+
'user_name' => '',
23+
'password' => '',
24+
'client_id' => Client::genClientID(),
25+
'keep_alive' => 20,
26+
];
1627

17-
$config = [
18-
'host' => 'test.mosquitto.org',
19-
'port' => 8883,
20-
'time_out' => 5,
21-
'user_name' => '',
22-
'password' => '',
23-
'client_id' => Client::genClientID(),
24-
'keep_alive' => 20,
25-
];
28+
$swooleConfig = [
29+
'open_mqtt_protocol' => true,
30+
'package_max_length' => 2 * 1024 * 1024,
31+
'ssl_cafile' => SSL_CERTS_DIR . '/mosquitto.org.crt', // https://test.mosquitto.org/ssl/mosquitto.org.crt
32+
'ssl_allow_self_signed' => true,
33+
'ssl_verify_peer' => true,
34+
];
2635

27-
Coroutine\run(
28-
function () use ($config) {
29-
$client = new Client(
30-
$config, [
31-
'open_mqtt_protocol' => true,
32-
'package_max_length' => 2 * 1024 * 1024,
33-
'ssl_cafile' => __DIR__ . '/mosquitto.org.crt', // https://test.mosquitto.org/ssl/mosquitto.org.crt
34-
'ssl_allow_self_signed' => true,
35-
'ssl_verify_peer' => true,
36-
], SWOOLE_SOCK_TCP | SWOOLE_SSL
37-
);
38-
$will = [
39-
'topic' => 'testtopic/#',
40-
'qos' => 1,
41-
'retain' => 0,
42-
'message' => '' . time(),
43-
];
44-
while (!$client->connect(false, $will)) {
45-
\Swoole\Coroutine::sleep(3);
46-
$client->connect(true, $will);
36+
$client = new Client($config, $swooleConfig, SWOOLE_SOCK_TCP | SWOOLE_SSL);
37+
38+
while (!$data = $client->connect()) {
39+
Coroutine::sleep(3);
40+
$client->connect();
41+
}
42+
$topics['testtopic/#'] = 0;
43+
$timeSincePing = time();
44+
$client->subscribe($topics);
45+
while (true) {
46+
$buffer = $client->recv();
47+
var_dump($buffer);
48+
if ($buffer && $buffer !== true) {
49+
$timeSincePing = time();
4750
}
48-
$topics['testtopic/#'] = 0;
49-
$timeSincePing = time();
50-
$client->subscribe($topics);
51-
while (true) {
52-
$buffer = $client->recv();
53-
var_dump($buffer);
54-
if ($buffer && $buffer !== true) {
51+
if (isset($config['keep_alive']) && $timeSincePing < (time() - $config['keep_alive'])) {
52+
$buffer = $client->ping();
53+
if ($buffer) {
54+
echo 'send ping success' . PHP_EOL;
5555
$timeSincePing = time();
56-
}
57-
if (isset($config['keep_alive']) && $timeSincePing < (time() - $config['keep_alive'])) {
58-
$buffer = $client->ping();
59-
if ($buffer) {
60-
echo 'send ping success' . PHP_EOL;
61-
$timeSincePing = time();
62-
} else {
63-
$client->close();
64-
break;
65-
}
56+
} else {
57+
$client->close();
58+
break;
6659
}
6760
}
6861
}
69-
);
62+
});

0 commit comments

Comments
 (0)