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

Commit 8fab252

Browse files
committed
Get server information after connection.
1 parent 84d659a commit 8fab252

3 files changed

Lines changed: 390 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: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,37 @@ 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+
var_dump($connectionResponse);
258+
$this->serverInfo = new ServerInfo($connectionResponse);
259+
}
260+
261+
/**
262+
* Returns current connected server ID.
263+
*
264+
* @return string Server ID.
265+
*/
266+
public function connectedServerID()
267+
{
268+
return $this->serverInfo->getServerID();
269+
}
270+
240271
/**
241272
* Constructor.
242273
*
@@ -390,11 +421,12 @@ public function connect($timeout = null)
390421

391422
$msg = 'CONNECT '.$this->options;
392423
$this->send($msg);
393-
// $infoResponse = $this->receive();
394424
$connectResponse = $this->receive();
395425

396426
if ($this->isErrorResponse($connectResponse) === true) {
397427
throw Exception::forFailedConnection($connectResponse);
428+
} else {
429+
$this->processServerInfo($connectResponse);
398430
}
399431

400432
$this->ping();

0 commit comments

Comments
 (0)