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

Commit 740e19e

Browse files
committed
Merge branch 'release/0.3.0'
2 parents 90b1369 + 235ab8b commit 740e19e

9 files changed

Lines changed: 346 additions & 36 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ $callback = function($payload)
6868
};
6969
$client->subscribe("foo", $callback);
7070

71+
# Request
72+
$c->request('sayhello', 'Marty McFly', function ($response) {
73+
echo $response->getBody();
74+
});
75+
76+
# Responding to requests
77+
$sid = $c->subscribe("sayhello", function ($res) {
78+
$res->reply("Hello, " . $res->getBody() . " !!!");
79+
});
80+
81+
82+
7183
# Wait for 1 message
7284
$client->wait(1);
7385
```

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.0
1+
0.3.0

examples/reqres/req.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require_once __DIR__ . "/../../vendor/autoload.php";
3+
4+
$connectionOptions = new \Nats\ConnectionOptions();
5+
$connectionOptions->setHost('localhost')->setPort(4222);
6+
7+
$c = new Nats\Connection($connectionOptions);
8+
$c->connect();
9+
10+
$c->request(
11+
'sayhello',
12+
'Marty McFly',
13+
function ($response) {
14+
echo $response->getBody();
15+
}
16+
);

examples/reqres/res.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once __DIR__ . "/../../vendor/autoload.php";
3+
4+
$connectionOptions = new \Nats\ConnectionOptions();
5+
$connectionOptions
6+
->setHost('localhost')
7+
->setPort(4222);
8+
$c = new Nats\Connection($connectionOptions);
9+
$c->connect();
10+
11+
$sid = $c->subscribe(
12+
"sayhello",
13+
function ($res) {
14+
$res->reply("Hello, " . $res->getBody() . " !!!");
15+
}
16+
);
17+
18+
$c->wait(2);
19+
20+
$c->unsubscribe($sid);

src/Connection.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getSubscriptions()
8585
}
8686

8787
/**
88-
* Connection options object
88+
* Connection options object.
8989
*
9090
* @var ConnectionOptions|null
9191
*/
@@ -118,6 +118,7 @@ public function __construct(ConnectionOptions $options = null)
118118
* Sends data thought the stream.
119119
*
120120
* @param string $payload Message data.
121+
*
121122
* @return void
122123
*/
123124
private function send($payload)
@@ -147,6 +148,7 @@ private function receive($len = null)
147148
*
148149
* @param string $address Server url string.
149150
* @param integer $timeout Number of seconds until the connect() system call should timeout.
151+
*
150152
* @return resource
151153
* @throws \Exception Exception raised if connection fails.
152154
*/
@@ -177,6 +179,7 @@ public function isConnected()
177179
* Connect to server.
178180
*
179181
* @param integer $timeout Number of seconds until the connect() system call should timeout.
182+
*
180183
* @throws \Exception Exception raised if connection fails.
181184
* @return void
182185
*/
@@ -199,11 +202,34 @@ public function ping()
199202
$this->pings += 1;
200203
}
201204

205+
/**
206+
* Request does a request and executes a callback with the response.
207+
*
208+
* @param string $subject Message topic.
209+
* @param string $payload Message data.
210+
* @param mixed $callback Closure to be executed as callback.
211+
* @param integer $wait Number of messages to wait for.
212+
*
213+
* @return void
214+
*/
215+
public function request($subject, $payload, $callback, $wait = 1)
216+
{
217+
$inbox = uniqid('_INBOX.');
218+
$this->subscribe($inbox, $callback);
219+
220+
$msg = 'PUB '.$subject.' '.$inbox.' '.strlen($payload);
221+
$this->send($msg . "\r\n" . $payload);
222+
$this->pubs += 1;
223+
224+
$this->wait($wait);
225+
}
226+
202227
/**
203228
* Publish publishes the data argument to the given subject.
204229
*
205230
* @param string $subject Message topic.
206231
* @param string $payload Message data.
232+
*
207233
* @return void
208234
*/
209235
public function publish($subject, $payload)
@@ -216,8 +242,9 @@ public function publish($subject, $payload)
216242
/**
217243
* Subscribes to an specific event given a subject.
218244
*
219-
* @param string $subject Message topic.
220-
* @param resource $callback Closure to be executed as callback.
245+
* @param string $subject Message topic.
246+
* @param mixed $callback Closure to be executed as callback.
247+
*
221248
* @return string
222249
*/
223250
public function subscribe($subject, $callback)
@@ -234,6 +261,7 @@ public function subscribe($subject, $callback)
234261
* Unsubscribe from a event given a subject.
235262
*
236263
* @param string $sid Subscription ID.
264+
*
237265
* @return void
238266
*/
239267
public function unsubscribe($sid)
@@ -260,29 +288,38 @@ private function handlePING()
260288
* @param string $line Message command from NATS.
261289
*
262290
* @return \Exception|void
291+
* @codeCoverageIgnore
263292
*/
264293
private function handleMSG($line)
265294
{
266295
$parts = explode(' ', $line);
296+
$subject = null;
267297
$length = $parts[3];
268298
$sid = $parts[2];
269299

300+
if (count($parts) == 5) {
301+
$length = $parts[5];
302+
$subject = $parts[3];
303+
}
304+
270305
$payload = $this->receive($length);
306+
$msg = new Message($subject, $payload, $sid, $this);
271307

272308
$func = $this->subscriptions[$sid];
273309
if (is_callable($func)) {
274-
$func($payload);
310+
$func($msg);
275311
} else {
276312
return new \Exception('not callable');
277313
}
278-
314+
279315
return;
280316
}
281317

282318
/**
283319
* Waits for messages.
284320
*
285321
* @param integer $quantity Number of messages to wait for.
322+
*
286323
* @return resource $connection Connection object
287324
*/
288325
public function wait($quantity = 0)

0 commit comments

Comments
 (0)