Skip to content

Commit f322a94

Browse files
authored
Merge pull request #23 from jjok/simplify-emit-events
Simplify emitting events
2 parents c511e59 + 800b625 commit f322a94

11 files changed

+36
-35
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ $loop->run();
4949
```php
5050
<?php
5151

52+
use oliverlorenz\reactphpmqtt\packet\Publish;
53+
5254
require __DIR__ . '/../vendor/autoload.php';
5355

5456
$config = include('config.php');
@@ -63,7 +65,7 @@ $connector = new oliverlorenz\reactphpmqtt\Connector($loop, $resolver, $version)
6365

6466
$p = $connector->create($config['server'], $config['port'], $config['options']);
6567
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
66-
$stream->on('PUBLISH', function(\oliverlorenz\reactphpmqtt\packet\Publish $message) {
68+
$stream->on(Publish::EVENT, function(Publish $message) {
6769
print_r($message);
6870
});
6971

@@ -74,13 +76,13 @@ $p->then(function(\React\Stream\Stream $stream) use ($connector) {
7476
$loop->run();
7577
```
7678

77-
# Run tests
79+
## Run tests
7880

7981
./vendor/bin/phpunit -c ./tests/phpunit.xml ./tests
8082

8183

82-
# Troubleshooting
84+
## Troubleshooting
8385

84-
## Why does the connect to localhost:1883 not work?
86+
### Why does the connect to localhost:1883 not work?
8587

8688
The answer is simple: In the example is the DNS 8.8.8.8 configured. Your local server is not visible for them, so you can't connect.

examples/connectSubscribe.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use oliverlorenz\reactphpmqtt\packet\Publish;
4+
35
require __DIR__ . '/../vendor/autoload.php';
46

57
$config = include('config.php');
@@ -14,7 +16,7 @@
1416

1517
$p = $connector->create($config['server'], $config['port'], $config['options']);
1618
$p->then(function(\React\Stream\Stream $stream) use ($connector) {
17-
$stream->on('PUBLISH', function(\oliverlorenz\reactphpmqtt\packet\Publish $message) {
19+
$stream->on(Publish::EVENT, function(Publish $message) {
1820
printf(
1921
'Received payload "%s" for topic "%s"%s',
2022
$message->getPayload(),

src/Connector.php

+5-22
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
use oliverlorenz\reactphpmqtt\packet\Factory;
1616
use oliverlorenz\reactphpmqtt\packet\MessageHelper;
1717
use oliverlorenz\reactphpmqtt\packet\PingRequest;
18-
use oliverlorenz\reactphpmqtt\packet\PingResponse;
1918
use oliverlorenz\reactphpmqtt\packet\Publish;
20-
use oliverlorenz\reactphpmqtt\packet\PublishReceived;
21-
use oliverlorenz\reactphpmqtt\packet\PublishRelease;
2219
use oliverlorenz\reactphpmqtt\packet\Subscribe;
2320
use oliverlorenz\reactphpmqtt\packet\SubscribeAck;
2421
use oliverlorenz\reactphpmqtt\packet\Unsubscribe;
@@ -88,31 +85,17 @@ private function listenForPackets(Stream $stream)
8885
foreach ($messages as $data) {
8986
try {
9087
$message = Factory::getByMessage($this->version, $data);
91-
echo "received:\t" . get_class($message) . "\n";
88+
$stream->emit($message::EVENT, [$message]);
9289

93-
if ($message instanceof ConnectionAck) {
94-
$stream->emit('CONNECTION_ACK', array($message));
95-
} elseif ($message instanceof PingResponse) {
96-
$stream->emit('PING_RESPONSE', array($message));
97-
} elseif ($message instanceof Publish) {
98-
$stream->emit('PUBLISH', array($message));
99-
} elseif ($message instanceof PublishReceived) {
100-
$stream->emit('PUBLISH_RECEIVED', array($message));
101-
} elseif ($message instanceof PublishRelease) {
102-
$stream->emit('PUBLISH_RELEASE', array($message));
103-
} elseif ($message instanceof UnsubscribeAck) {
104-
$stream->emit('UNSUBSCRIBE_ACK', array($message));
105-
} elseif ($message instanceof SubscribeAck) {
106-
$stream->emit('SUBSCRIBE_ACK', array($message));
107-
}
90+
echo "received:\t" . get_class($message) . "\n";
10891
} catch (\InvalidArgumentException $ex) {
10992

11093
}
11194
}
11295
});
11396

11497
$deferred = new Deferred();
115-
$stream->on('CONNECTION_ACK', function($message) use ($stream, $deferred) {
98+
$stream->on(ConnectionAck::EVENT, function($message) use ($stream, $deferred) {
11699
$deferred->resolve($stream);
117100
});
118101

@@ -183,7 +166,7 @@ public function subscribe(Stream $stream, $topic, $qos = 0)
183166
$this->sendPacketToStream($stream, $packet);
184167

185168
$deferred = new Deferred();
186-
$stream->on('SUBSCRIBE_ACK', function($message) use ($stream, $deferred) {
169+
$stream->on(SubscribeAck::EVENT, function($message) use ($stream, $deferred) {
187170
$deferred->resolve($stream);
188171
});
189172

@@ -202,7 +185,7 @@ public function unsubscribe(Stream $stream, $topic)
202185
$this->sendPacketToStream($stream, $packet);
203186

204187
$deferred = new Deferred();
205-
$stream->on('UNSUBSCRIBE_ACK', function($message) use ($stream, $deferred) {
188+
$stream->on(UnsubscribeAck::EVENT, function($message) use ($stream, $deferred) {
206189
$deferred->resolve($stream);
207190
});
208191

src/packet/ConnectionAck.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
* The CONNACK Packet is the packet sent by the Server in response to
1212
* a CONNECT Packet received from a Client.
1313
*/
14-
class ConnectionAck extends ControlPacket {
14+
class ConnectionAck extends ControlPacket
15+
{
16+
const EVENT = 'CONNECTION_ACK';
1517

1618
public static function getControlPacketType()
1719
{

src/packet/Factory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Factory {
1313
* @param $version
1414
* @param $input
1515
* @throws \InvalidArgumentException
16-
* @return ControlPacket
16+
* @return ConnectionAck|PingResponse|SubscribeAck|Publish|PublishComplete|PublishRelease|PublishReceived
1717
*/
1818
public static function getByMessage($version, $input)
1919
{

src/packet/PingResponse.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
* A PINGRESP Packet is sent by the Server to the Client in response
1212
* to a PINGREQ Packet. It indicates that the Server is alive.
1313
*/
14-
class PingResponse extends ControlPacket {
14+
class PingResponse extends ControlPacket
15+
{
16+
const EVENT = 'PING_RESPONSE';
1517

1618
public static function getControlPacketType()
1719
{

src/packet/Publish.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
* A PUBLISH Control Packet is sent from a Client to a Server or from
1414
* Server to a Client to transport an Application Message.
1515
*/
16-
class Publish extends ControlPacket {
16+
class Publish extends ControlPacket
17+
{
18+
const EVENT = 'PUBLISH';
1719

1820
protected $messageId;
1921

src/packet/PublishReceived.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
* A PUBREC Packet is the response to a PUBLISH Packet with QoS 2.
1212
* It is the second packet of the QoS 2 protocol exchange.
1313
*/
14-
class PublishReceived extends ControlPacket {
14+
class PublishReceived extends ControlPacket
15+
{
16+
const EVENT = 'PUBLISH_RECEIVED';
1517

1618
public static function getControlPacketType()
1719
{

src/packet/PublishRelease.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
* A PUBREL Packet is the response to a PUBREC Packet.
1212
* It is the third packet of the QoS 2 protocol exchange.
1313
*/
14-
class PublishRelease extends ControlPacket {
14+
class PublishRelease extends ControlPacket
15+
{
16+
const EVENT = 'PUBLISH_RELEASE';
1517

1618
public static function getControlPacketType()
1719
{

src/packet/SubscribeAck.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
* A SUBACK Packet is sent by the Server to the Client to confirm receipt
1212
* and processing of a SUBSCRIBE Packet.
1313
*/
14-
class SubscribeAck extends ControlPacket {
14+
class SubscribeAck extends ControlPacket
15+
{
16+
const EVENT = 'SUBSCRIBE_ACK';
1517

1618
public static function getControlPacketType()
1719
{

src/packet/UnsubscribeAck.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
* The UNSUBACK Packet is sent by the Server to the Client to confirm
1212
* receipt of an UNSUBSCRIBE Packet.
1313
*/
14-
class UnsubscribeAck extends ControlPacket {
14+
class UnsubscribeAck extends ControlPacket
15+
{
16+
const EVENT = 'UNSUBSCRIBE_ACK';
1517

1618
public static function getControlPacketType()
1719
{

0 commit comments

Comments
 (0)