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

Commit 4115fdd

Browse files
committed
Fixed errors with replies on request/response
1 parent d9a6fdc commit 4115fdd

9 files changed

Lines changed: 102 additions & 72 deletions

File tree

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,38 @@ $sid = $client->subscribe("sayhello", function ($res) {
7878
$res->reply("Hello, " . $res->getBody() . " !!!");
7979
});
8080

81+
# Wait for 1 message
82+
$client->wait(1);
83+
```
84+
85+
### Encoded Connections
86+
87+
```php
88+
$encoder = new \Nats\Encoders\JSONEncoder();
89+
$options = new \Nats\ConnectionOptions();
90+
$client = new \Nats\EncodedConnection($options, $encoder);
91+
$client->connect();
92+
93+
# Simple Publisher
94+
$client->publish("foo", array("one", "two"));
95+
96+
# Simple Subscriber
97+
$callback = function($payload)
98+
{
99+
printf("Data: %s\r\n", var_dump($payload));
100+
};
101+
$client->subscribe("foo", $callback);
102+
103+
# Request
104+
$client->request('sayhello', ['foo', 'Marty McFly'], function ($response) {
105+
echo 'Hello '.$response->getBody()[1].' !!!';
106+
});
107+
108+
# Responding to requests
109+
$sid = $client->subscribe("sayhello", function ($res) {
110+
$res->reply("Hello, " . $res->getBody() . " !!!");
111+
});
112+
81113

82114

83115
# Wait for 1 message

examples/pubsub/jsonencodedpub.php

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
require_once __DIR__.'/../../vendor/autoload.php';
3+
4+
$encoder = new \Nats\Encoders\JSONEncoder();
5+
$options = new \Nats\ConnectionOptions();
6+
$client = new \Nats\EncodedConnection($options, $encoder);
7+
$client->connect();
8+
9+
// Publish Subscribe
10+
11+
12+
# Simple Subscriber
13+
$callback = function($payload)
14+
{
15+
printf("Data: %s\r\n", $payload->getBody()[1]);
16+
};
17+
$client->subscribe("foo", $callback);
18+
19+
# Simple Publisher
20+
$client->publish("foo", ["foo", "bar"]);
21+
22+
# Wait for 1 message
23+
$client->wait(1);
24+
25+
// Request Response
26+
27+
28+
# Responding to requests
29+
$sid = $client->subscribe("sayhello", function ($message) {
30+
$message->reply("Reply: Hello, " . $message->getBody()[1] . " !!!");
31+
});
32+
33+
# Request
34+
$client->request('sayhello', ["foo", "McFly"], function ($message) {
35+
echo $message->getBody();
36+
});
37+

examples/pubsub/jsonencodedsub.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/pubsub/pub.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/pubsub/pubsub.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
require_once __DIR__.'/../../vendor/autoload.php';
3+
4+
$client = new \Nats\Connection();
5+
$client->connect();
6+
7+
// Publish Subscribe
8+
9+
# Simple Subscriber
10+
$callback = function($message)
11+
{
12+
printf("Data: %s\r\n", $message->getBody());
13+
};
14+
$client->subscribe("foo", $callback);
15+
16+
# Simple Publisher
17+
$client->publish("foo", "foo bar");
18+
19+
# Wait for 1 message
20+
$client->wait(1);
21+
22+
// Request Response
23+
24+
# Responding to requests
25+
$sid = $client->subscribe("sayhello", function ($message) {
26+
$message->reply("Reply: Hello, " . $message->getBody() . " !!!");
27+
});
28+
29+
# Request
30+
$client->request('sayhello', 'Marty McFly', function ($message) {
31+
echo $message->getBody();
32+
});

examples/pubsub/sub.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Nats/Connection.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,6 @@ private function handleMSG($line)
366366
} else {
367367
throw Exception::forSubscriptionCallbackInvalid($sid);
368368
}
369-
370-
return;
371369
}
372370

373371
/**
@@ -436,7 +434,7 @@ public function request($subject, $payload, \Closure $callback)
436434
$this->send($msg."\r\n".$payload);
437435
$this->pubs += 1;
438436

439-
$this->wait(1);
437+
$this->wait(2);
440438
}
441439

442440
/**

test/EncodedConnectionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ function ($res) {
110110
'McFly',
111111
],
112112
function ($res) {
113-
var_dump($res);
114113
$this->assertEquals('Hello, McFly !!!', $res->getBody());
115114
}
116115
);

0 commit comments

Comments
 (0)