-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
ssl_ca_client.php
58 lines (53 loc) · 1.84 KB
/
ssl_ca_client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* This file is part of Simps.
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <[email protected]>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
include_once __DIR__ . '/bootstrap.php';
use Simps\MQTT\Client;
use Simps\MQTT\Config\ClientConfig;
use Swoole\Coroutine;
Coroutine\run(function () {
$swooleConfig = [
'open_mqtt_protocol' => true,
'package_max_length' => 2 * 1024 * 1024,
'ssl_allow_self_signed' => true,
'ssl_verify_peer' => true,
'ssl_cafile' => SSL_CERTS_DIR . '/mosquitto.org.crt', // https://test.mosquitto.org/ssl/mosquitto.org.crt
'ssl_key_file' => SSL_CERTS_DIR . '/client.key', // Please go to https://test.mosquitto.org/ssl to generate.
'ssl_cert_file' => SSL_CERTS_DIR . '/client.crt', // Please go to https://test.mosquitto.org/ssl to generate.
];
$config = new ClientConfig();
$config->setClientId(Client::genClientID())
->setKeepAlive(20)
->setUserName('')
->setPassword('')
->setDelay(3000) // 3s
->setMaxAttempts(5)
->setSwooleConfig($swooleConfig)
->setSockType(SWOOLE_SOCK_TCP | SWOOLE_SSL);
$client = new Client('test.mosquitto.org', 8884, $config);
$client->connect();
$topics['testtopic/#'] = 0;
$client->subscribe($topics);
$timeSincePing = time();
while (true) {
$buffer = $client->recv();
if ($buffer && $buffer !== true) {
var_dump($buffer);
$timeSincePing = time();
}
if ($timeSincePing <= (time() - $config->getKeepAlive())) {
$buffer = $client->ping();
if ($buffer) {
echo 'send ping success' . PHP_EOL;
$timeSincePing = time();
}
}
}
});