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

Commit 7c87071

Browse files
authored
Merge pull request repejota#106 from repejota/feature/connectionInfo
Feature/connection info
2 parents 84d659a + c9ac267 commit 7c87071

3 files changed

Lines changed: 389 additions & 22 deletions

File tree

examples/simpleclient/main.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,9 @@
33

44
use Nats\Connection as NatsClient;
55

6-
$client = new NatsClient();
7-
$client->connect();
6+
$nc = new NatsClient();
7+
$nc->connect();
88

9-
printf('Connected to NATS.'.PHP_EOL);
9+
printf('Connected to NATS at %s'.PHP_EOL, $nc->connectedServerId());
1010

11-
// Simple Subscriber.
12-
$client->subscribe(
13-
'foo',
14-
function ($message) {
15-
printf("Data: %s\r\n".PHP_EOL, $message->getBody());
16-
}
17-
);
18-
printf('Subscribed to "foo" messages.'.PHP_EOL);
19-
20-
// Wait for messages.
21-
printf('Wait for messages on %s subscriptions'.PHP_EOL, $client->subscriptionsCount());
22-
while (true) {
23-
$client->wait();
24-
25-
printf('Reconnecting ...'.PHP_EOL);
26-
$client->reconnect();
27-
printf('Reconnected %s subscriptions'.PHP_EOL, $client->subscriptionsCount());
28-
}
11+
$nc->wait();

src/Nats/Connection.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,36 @@ function () {
237237
return $fp;
238238
}
239239

240+
/**
241+
* Server information.
242+
*
243+
* @var mixed
244+
*/
245+
private $serverInfo;
246+
247+
248+
/**
249+
* Process information returned by the server after connection.
250+
*
251+
* @param string $connectionResponse INFO message.
252+
*
253+
* @return void
254+
*/
255+
private function processServerInfo($connectionResponse)
256+
{
257+
$this->serverInfo = new ServerInfo($connectionResponse);
258+
}
259+
260+
/**
261+
* Returns current connected server ID.
262+
*
263+
* @return string Server ID.
264+
*/
265+
public function connectedServerID()
266+
{
267+
return $this->serverInfo->getServerID();
268+
}
269+
240270
/**
241271
* Constructor.
242272
*
@@ -390,11 +420,12 @@ public function connect($timeout = null)
390420

391421
$msg = 'CONNECT '.$this->options;
392422
$this->send($msg);
393-
// $infoResponse = $this->receive();
394423
$connectResponse = $this->receive();
395424

396425
if ($this->isErrorResponse($connectResponse) === true) {
397426
throw Exception::forFailedConnection($connectResponse);
427+
} else {
428+
$this->processServerInfo($connectResponse);
398429
}
399430

400431
$this->ping();

0 commit comments

Comments
 (0)