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

Commit c6f4324

Browse files
committed
callbacks on request to handle the response
1 parent d1009ec commit c6f4324

5 files changed

Lines changed: 26 additions & 16 deletions

File tree

src/Nats/Connection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,16 @@ public function ping()
421421
*
422422
* @param string $subject Message topic.
423423
* @param string $payload Message data.
424+
* @param \Closure $callback Closure to be executed as callback.
424425
*
425426
* @return void
426427
*/
427-
public function request($subject, $payload)
428+
public function request($subject, $payload, \Closure $callback)
428429
{
429430
$inbox = uniqid('_INBOX.');
430431
$this->subscribe(
431432
$inbox,
432-
function ($message) {
433-
}
433+
$callback
434434
);
435435
$msg = 'PUB '.$subject.' '.$inbox.' '.strlen($payload);
436436
$this->send($msg."\r\n".$payload);

src/Nats/EncodedConnection.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ public function __construct(ConnectionOptions $options = null, \Nats\Encoders\En
3434
*
3535
* @param string $subject Message topic.
3636
* @param string $payload Message data.
37+
* @param \Closure $callback Closure to be executed as callback.
3738
*
3839
* @return void
3940
*/
40-
public function request($subject, $payload)
41+
public function request($subject, $payload, \Closure $callback)
4142
{
4243
$payload = $this->encoder->encode($payload);
43-
parent::request($subject, $payload);
44+
parent::request($subject, $payload, $callback);
4445
}
4546

4647
/**
@@ -63,15 +64,15 @@ public function publish($subject, $payload = null)
6364
* @param string $subject Message topic.
6465
* @param \Closure $callback Closure to be executed as callback.
6566
*
66-
* @return void
67+
* @return string
6768
*/
6869
public function subscribe($subject, \Closure $callback)
6970
{
7071
$c = function ($message) use ($callback) {
7172
$message->setBody($this->encoder->decode($message->getBody()));
7273
$callback($message);
7374
};
74-
parent::subscribe($subject, $c);
75+
return parent::subscribe($subject, $c);
7576
}
7677

7778
/**

src/Nats/Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Message
1919
*
2020
* @var string
2121
*/
22-
private $body;
22+
public $body;
2323

2424
/**
2525
* Message Ssid.

test/ConnectionTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ function ($res) {
113113

114114
$this->c->request(
115115
'sayhello'.$i,
116-
'McFly'
116+
'McFly',
117+
function ($res) {
118+
$this->assertEquals('Hello, McFly !!!', $res->getBody());
119+
}
117120
);
118121

119122
$i++;
@@ -150,9 +153,8 @@ function ($res) use ($contentLen, $contentSum) {
150153
$this->c->request(
151154
'saybighello'.$i,
152155
$content,
153-
function ($message) use ($contentLen) {
154-
$this->assertNotNull($message);
155-
$this->assertEquals($message->getBody(), $contentLen);
156+
function ($res) {
157+
156158
}
157159
);
158160

@@ -173,8 +175,8 @@ public function testSetStreamTimeout()
173175
$this->c->request(
174176
'nonexistantsubject',
175177
'test',
176-
function ($message) {
177-
$this->fail('should never have gotten here');
178+
function ($res) {
179+
178180
}
179181
);
180182
$timeTaken = (time() - $before);

test/EncodedConnectionTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ function ($res) {
8282

8383
$this->c->request(
8484
'sayhello',
85-
'McFly'
85+
'McFly',
86+
function ($res) {
87+
$this->assertEquals('Hello, McFly !!!', $res->getBody());
88+
}
8689
);
8790
}
8891

@@ -105,7 +108,11 @@ function ($res) {
105108
[
106109
'foo',
107110
'McFly',
108-
]
111+
],
112+
function ($res) {
113+
var_dump($res);
114+
$this->assertEquals('Hello, McFly !!!', $res->getBody());
115+
}
109116
);
110117
}
111118
}

0 commit comments

Comments
 (0)