Skip to content

Commit 027016f

Browse files
committed
Update to react/socket 0.8
1 parent 07cf48d commit 027016f

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"php": "~5.6|~7.0",
2121
"binsoul/net-mqtt": "~0.2",
2222
"react/promise": "~2.0",
23-
"react/socket-client": "0.5.x"
23+
"react/socket": "~0.8"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "~4.0||~5.0",

src/ReactMqttClient.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
use React\EventLoop\LoopInterface;
2727
use React\Promise\ExtendedPromiseInterface;
2828
use React\Promise\RejectedPromise;
29-
use React\SocketClient\ConnectorInterface;
30-
use React\Stream\Stream;
29+
use React\Socket\ConnectorInterface;
30+
use React\Stream\DuplexStreamInterface;
3131

3232
/**
3333
* Connects to a MQTT broker and subscribes to topics or publishes messages.
@@ -52,7 +52,7 @@ class ReactMqttClient extends EventEmitter
5252
private $connector;
5353
/** @var LoopInterface */
5454
private $loop;
55-
/** @var Stream */
55+
/** @var DuplexStreamInterface */
5656
private $stream;
5757
/** @var StreamParser */
5858
private $parser;
@@ -147,7 +147,7 @@ public function isConnected()
147147
/**
148148
* Returns the underlying stream or null if the client is not connected.
149149
*
150-
* @return Stream|null
150+
* @return DuplexStreamInterface|null
151151
*/
152152
public function getStream()
153153
{
@@ -187,7 +187,7 @@ public function connect($host, $port = 1883, Connection $connection = null, $tim
187187
$deferred = new Deferred();
188188

189189
$this->establishConnection($this->host, $this->port, $timeout)
190-
->then(function (Stream $stream) use ($connection, $deferred, $timeout) {
190+
->then(function (DuplexStreamInterface $stream) use ($connection, $deferred, $timeout) {
191191
$this->stream = $stream;
192192

193193
$this->emit('open', [$connection, $this]);
@@ -377,19 +377,15 @@ function () use ($deferred, $timeout) {
377377
}
378378
);
379379

380-
$this->connector->create($host, $port)
380+
$this->connector->connect($host.':'.$port)
381381
->always(function () use ($timer) {
382382
$this->loop->cancelTimer($timer);
383383
})
384-
->then(function (Stream $stream) use ($deferred, $timeout) {
384+
->then(function (DuplexStreamInterface $stream) use ($deferred) {
385385
$stream->on('data', function ($data) {
386386
$this->handleReceive($data);
387387
});
388388

389-
$stream->getBuffer()->on('full-drain', function () {
390-
$this->handleSend();
391-
});
392-
393389
$stream->on('close', function () {
394390
$this->handleClose();
395391
});
@@ -467,6 +463,8 @@ private function handleReceive($data)
467463
if ($flowCount > count($this->receivingFlows)) {
468464
$this->receivingFlows = array_values($this->receivingFlows);
469465
}
466+
467+
$this->handleSend();
470468
}
471469

472470
/**
@@ -603,11 +601,12 @@ private function startFlow(Flow $flow, $isSilent = false)
603601
$internalFlow = new ReactFlow($flow, $deferred, $packet, $isSilent);
604602

605603
if ($packet !== null) {
606-
if ($this->stream->getBuffer()->listening) {
604+
if ($this->writtenFlow !== null) {
607605
$this->sendingFlows[] = $internalFlow;
608606
} else {
609607
$this->stream->write($packet);
610608
$this->writtenFlow = $internalFlow;
609+
$this->handleSend();
611610
}
612611
} else {
613612
$this->loop->nextTick(function () use ($internalFlow) {
@@ -635,11 +634,12 @@ private function continueFlow(ReactFlow $flow, Packet $packet)
635634
}
636635

637636
if ($response !== null) {
638-
if ($this->stream->getBuffer()->listening) {
637+
if ($this->writtenFlow !== null) {
639638
$this->sendingFlows[] = $flow;
640639
} else {
641640
$this->stream->write($response);
642641
$this->writtenFlow = $flow;
642+
$this->handleSend();
643643
}
644644
} elseif ($flow->isFinished()) {
645645
$this->loop->nextTick(function () use ($flow) {

tests/Integration/ReactMqttClientTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
use React\EventLoop\Factory as EventLoopFactory;
1414
use React\Dns\Resolver\Factory as DNSResolverFactory;
1515
use React\EventLoop\LoopInterface;
16-
use React\SocketClient\DnsConnector;
17-
use React\SocketClient\SecureConnector;
18-
use React\SocketClient\TcpConnector;
16+
use React\Socket\Connector;
1917

2018
/**
2119
* Tests the ReactMqttClient class.
@@ -40,7 +38,7 @@ class ReactMqttClientTest extends \PHPUnit_Framework_TestCase
4038
*
4139
* @var string
4240
*/
43-
const HOSTNAME = 'iot.eclipse.org';
41+
const HOSTNAME = 'tls://iot.eclipse.org';
4442

4543
/**
4644
* Port.
@@ -170,10 +168,7 @@ private function log($message, $clientName = '')
170168
*/
171169
private function buildClient($name = '', $isPrimary = true)
172170
{
173-
$connector = new DnsConnector(new TcpConnector($this->loop), $this->resolver);
174-
if (self::SECURE) {
175-
$connector = new SecureConnector($connector, $this->loop);
176-
}
171+
$connector = new Connector($this->loop, ['timeout' => false]);
177172

178173
$client = new ReactMqttClient($connector, $this->loop);
179174
if ($isPrimary) {

0 commit comments

Comments
 (0)