@@ -22,80 +22,103 @@ Connect to a public broker and run forever.
22
22
<?php
23
23
24
24
use BinSoul\Net\Mqtt\Client\React\ReactMqttClient;
25
- use React\SocketClient\Connector;
25
+ use BinSoul\Net\Mqtt\Connection;
26
+ use BinSoul\Net\Mqtt\DefaultMessage;
27
+ use BinSoul\Net\Mqtt\DefaultSubscription;
28
+ use BinSoul\Net\Mqtt\Message;
29
+ use BinSoul\Net\Mqtt\Subscription;
30
+ use React\SocketClient\DnsConnector;
31
+ use React\SocketClient\TcpConnector;
26
32
27
33
include 'vendor/autoload.php';
28
34
29
35
// Setup client
30
36
$loop = React\EventLoop\Factory::create();
31
37
$dnsResolverFactory = new React\Dns\Resolver\Factory();
32
- $connector = new Connector( $loop, $dnsResolverFactory->createCached('8.8.8.8', $loop));
38
+ $connector = new DnsConnector(new TcpConnector( $loop) , $dnsResolverFactory->createCached('8.8.8.8', $loop));
33
39
$client = new ReactMqttClient($connector, $loop);
34
40
35
41
// Bind to events
36
- $client->on('connect', function () {
37
- echo "Connected.\n";
42
+ $client->on('open', function () use ($client) {
43
+ // Network connection established
44
+ echo sprintf("Open: %s:%s\n", $client->getHost(), $client->getPort());
38
45
});
39
46
40
- $client->on('disconnect', function () {
41
- echo "Disconnected.\n";
47
+ $client->on('close', function () use ($client, $loop) {
48
+ // Network connection closed
49
+ echo sprintf("Close: %s:%s\n", $client->getHost(), $client->getPort());
50
+
51
+ $loop->stop();
52
+ });
53
+
54
+ $client->on('connect', function (Connection $connection) {
55
+ // Broker connected
56
+ echo sprintf("Connect: client=%s\n", $connection->getClientID());
42
57
});
43
58
44
- $client->on('message', function ($topic, $message, $isDuplicate, $isRetained) {
45
- echo 'Incoming: '.$topic.' => '.mb_strimwidth($message, 0, 50, '...');
46
-
47
- if ($isDuplicate) {
59
+ $client->on('disconnect', function (Connection $connection) {
60
+ // Broker disconnected
61
+ echo sprintf("Disconnect: client=%s\n", $connection->getClientID());
62
+ });
63
+
64
+ $client->on('message', function (Message $message) {
65
+ // Incoming message
66
+ echo 'Message';
67
+
68
+ if ($message->isDuplicate()) {
48
69
echo ' (duplicate)';
49
70
}
50
71
51
- if ($isRetained) {
72
+ if ($message-> isRetained() ) {
52
73
echo ' (retained)';
53
74
}
54
75
76
+ echo ': '.$message->getTopic().' => '.mb_strimwidth($message->getPayload(), 0, 50, '...');
55
77
echo "\n";
56
78
});
57
79
58
80
$client->on('warning', function (\Exception $e) {
59
- echo $e->getMessage();
81
+ echo sprintf("Warning: %s\n", $e->getMessage() );
60
82
});
61
83
62
- $client->on('error', function (\Exception $e) {
63
- echo $e->getMessage();
64
- die();
84
+ $client->on('error', function (\Exception $e) use ($loop) {
85
+ echo sprintf("Error: %s\n", $e->getMessage());
86
+
87
+ $loop->stop();
65
88
});
66
89
67
90
// Connect to broker
68
91
$client->connect('test.mosquitto.org')->then(
69
- function (ReactMqttClient $client) {
70
- // Subscribe to all topics below "sensors"
71
- $client->subscribe('sensors/#' )
72
- ->then(function ($topic ) {
73
- echo sprintf("Subscribed to topic '%s'. \n", $topic );
92
+ function () use ( $client) {
93
+ // Subscribe to all topics
94
+ $client->subscribe(new DefaultSubscription('#') )
95
+ ->then(function (Subscription $subscription ) {
96
+ echo sprintf("Subscribe: %s \n", $subscription->getFilter() );
74
97
})
75
98
->otherwise(function (\Exception $e) {
76
- echo $e->getMessage();
99
+ echo sprintf("Error: %s\n", $e->getMessage() );
77
100
});
78
101
79
102
// Publish humidity once
80
- $client->publish('sensors/humidity', '55 %', 1 )
81
- ->then(function ($value ) {
82
- echo sprintf("Published message '%s'. \n", $value );
103
+ $client->publish(new DefaultMessage( 'sensors/humidity', '55%') )
104
+ ->then(function (Message $message ) {
105
+ echo sprintf("Publish: %s => %s \n", $message->getTopic(), $message->getPayload() );
83
106
})
84
107
->otherwise(function (\Exception $e) {
85
- echo $e->getMessage();
108
+ echo sprintf("Error: %s\n", $e->getMessage() );
86
109
});
87
110
88
111
// Publish a random temperature every 10 seconds
89
112
$generator = function () {
90
113
return mt_rand(-20, 30);
91
114
};
92
115
93
- $client->publishPeriodically(10, 'sensors/temperature', $generator, 1 )
94
- ->progress(function ($value ) {
95
- echo sprintf("Published message '%s'. \n", $value );
116
+ $client->publishPeriodically(10, new DefaultMessage( 'sensors/temperature') , $generator)
117
+ ->progress(function (Message $message ) {
118
+ echo sprintf("Publish: %s => %s \n", $message->getTopic(), $message->getPayload() );
96
119
})
97
120
->otherwise(function (\Exception $e) {
98
- echo $e->getMessage();
121
+ echo sprintf("Error: %s\n", $e->getMessage() );
99
122
});
100
123
}
101
124
);
0 commit comments