Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 4959e9d

Browse files
committed
Merge branch 'release/0.8.6'
2 parents 574eaf7 + f618809 commit 4959e9d

10 files changed

Lines changed: 112 additions & 31 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.8.5
1+
0.8.6

examples/connect.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
<?php
22
require_once __DIR__.'/../vendor/autoload.php';
33

4-
$connectionOptions = new \Nats\ConnectionOptions(
5-
[
6-
'host' => '127.0.0.1',
7-
'port' => 4222,
8-
]
9-
);
10-
11-
$c = new Nats\Connection($connectionOptions);
4+
$c = new Nats\Connection();
125
$c->connect();
136
$c->close();

examples/ping.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@
99
$c = new Nats\Connection($connectionOptions);
1010
$c->connect();
1111

12+
13+
// TODO ping is not public.
1214
$c->ping();

examples/pubsub/pub.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
require_once __DIR__.'/../../vendor/autoload.php';
3+
4+
$client = new \Nats\Connection();
5+
$client->connect();
6+
7+
// Simple Publisher.
8+
$client->publish('foo', 'bar');
9+
$client->close();

examples/pubsub/pubsub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ function ($message) {
1818

1919
// Wait for 1 message.
2020
$client->wait(1);
21+
22+
$client->close();

examples/pubsub/sub.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
require_once __DIR__.'/../../vendor/autoload.php';
3+
4+
$client = new \Nats\Connection();
5+
$client->connect();
6+
7+
// Simple Subscriber.
8+
$client->subscribe(
9+
'foo',
10+
function ($message) {
11+
printf("Data: %s\r\n", $message->getBody());
12+
}
13+
);
14+
15+
$client->close();

examples/reqres/request.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
require_once __DIR__.'/../../vendor/autoload.php';
3+
4+
use Nats\Connection;
5+
6+
$client = new Connection();
7+
$client->connect();
8+
9+
// Request.
10+
$client->request(
11+
'foo',
12+
'Marty McFly',
13+
function ($message) {
14+
echo $message->getBody();
15+
}
16+
);
17+
18+
$client->close();

src/Nats/Connection.php

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,31 @@ class Connection
1515
{
1616

1717
/**
18-
* Number of PINGs.
18+
* Show DEBUG info?
1919
*
20-
* @var integer number of pings.
20+
* @var boolean $debug If debug is enabled.
2121
*/
22-
private $pings = 0;
22+
private $debug = false;
23+
2324

2425
/**
25-
* Chunk size in bytes to use when reading an stream of data.
26+
* Enable or disable debug mode.
2627
*
27-
* @var integer size of chunk.
28+
* @param boolean $debug If debug is enabled.
29+
*
30+
* @return void
2831
*/
29-
private $chunkSize = 1500;
32+
public function setDebug($debug)
33+
{
34+
$this->debug = $debug;
35+
}
36+
37+
/**
38+
* Number of PINGs.
39+
*
40+
* @var integer number of pings.
41+
*/
42+
private $pings = 0;
3043

3144

3245
/**
@@ -39,6 +52,13 @@ public function pingsCount()
3952
return $this->pings;
4053
}
4154

55+
/**
56+
* Chunk size in bytes to use when reading an stream of data.
57+
*
58+
* @var integer size of chunk.
59+
*/
60+
private $chunkSize = 1500;
61+
4262
/**
4363
* Number of messages published.
4464
*
@@ -319,6 +339,10 @@ private function send($payload)
319339
break;
320340
}
321341
}
342+
343+
if ($this->debug === true) {
344+
printf('>>>> %s', $msg);
345+
}
322346
}
323347

324348
/**
@@ -348,6 +372,10 @@ private function receive($len = 0)
348372
$line = fgets($this->streamSocket);
349373
}
350374

375+
if ($this->debug === true) {
376+
printf('<<<< %s\r\n', $line);
377+
}
378+
351379
return $line;
352380
}
353381

@@ -460,14 +488,12 @@ public function ping()
460488
public function request($subject, $payload, \Closure $callback)
461489
{
462490
$inbox = uniqid('_INBOX.');
463-
$this->subscribe(
491+
$sid = $this->subscribe(
464492
$inbox,
465493
$callback
466494
);
467-
$msg = 'PUB '.$subject.' '.$inbox.' '.strlen($payload);
468-
$this->send($msg."\r\n".$payload);
469-
$this->pubs += 1;
470-
495+
$this->unsubscribe($sid, 1);
496+
$this->publish($subject, $payload, $inbox);
471497
$this->wait(1);
472498
}
473499

@@ -509,30 +535,43 @@ public function queueSubscribe($subject, $queue, \Closure $callback)
509535
/**
510536
* Unsubscribe from a event given a subject.
511537
*
512-
* @param string $sid Subscription ID.
538+
* @param string $sid Subscription ID.
539+
* @param integer $quantity Quantity of messages.
513540
*
514541
* @return void
515542
*/
516-
public function unsubscribe($sid)
543+
public function unsubscribe($sid, $quantity = null)
517544
{
518545
$msg = 'UNSUB '.$sid;
546+
if ($quantity !== null) {
547+
$msg = $msg.' '.$quantity;
548+
}
549+
519550
$this->send($msg);
520-
unset($this->subscriptions[$sid]);
551+
if ($quantity === null) {
552+
unset($this->subscriptions[$sid]);
553+
}
521554
}
522555

523556
/**
524557
* Publish publishes the data argument to the given subject.
525558
*
526559
* @param string $subject Message topic.
527560
* @param string $payload Message data.
561+
* @param string $inbox Message inbox.
528562
*
529563
* @return void
530-
564+
*
531565
* @throws Exception If subscription not found.
532566
*/
533-
public function publish($subject, $payload = null)
567+
public function publish($subject, $payload = null, $inbox = null)
534568
{
535-
$msg = 'PUB '.$subject.' '.strlen($payload);
569+
$msg = 'PUB '.$subject;
570+
if ($inbox !== null) {
571+
$msg = $msg.' '.$inbox;
572+
}
573+
574+
$msg = $msg.' '.strlen($payload);
536575
$this->send($msg."\r\n".$payload);
537576
$this->pubs += 1;
538577
}

src/Nats/EncodedConnection.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Nats;
33

4+
use Nats\Encoders\Encoder;
5+
46
/**
57
* Class EncodedConnection
68
*
@@ -23,7 +25,7 @@ class EncodedConnection extends Connection
2325
* @param ConnectionOptions $options Connection options object.
2426
* @param \Nats\Encoders\Encoder|null $encoder Encoder to use with the payload.
2527
*/
26-
public function __construct(ConnectionOptions $options = null, \Nats\Encoders\Encoder $encoder = null)
28+
public function __construct(ConnectionOptions $options = null, Encoder $encoder = null)
2729
{
2830
$this->encoder = $encoder;
2931
parent::__construct($options);
@@ -49,13 +51,14 @@ public function request($subject, $payload, \Closure $callback)
4951
*
5052
* @param string $subject Message topic.
5153
* @param string $payload Message data.
54+
* @param string $inbox Message inbox.
5255
*
5356
* @return void
5457
*/
55-
public function publish($subject, $payload = null)
58+
public function publish($subject, $payload = null, $inbox = null)
5659
{
5760
$payload = $this->encoder->encode($payload);
58-
parent::publish($subject, $payload);
61+
parent::publish($subject, $payload, $inbox);
5962
}
6063

6164
/**

src/Nats/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public function getConn()
176176
*/
177177
public function reply($body)
178178
{
179-
$this->getConn()->publish(
180-
$this->getSubject(),
179+
$this->conn->publish(
180+
$this->subject,
181181
$body
182182
);
183183
}

0 commit comments

Comments
 (0)