Skip to content

Commit

Permalink
Add example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Mar 19, 2024
1 parent 64f61ac commit 1efa17e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,90 @@ Authentication isn't built-in, but can be implemented using middleware / interce
composer require amphp/rpc
```

## Usage

### Server

```php
<?php declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\SocketHttpServer;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Amp\Rpc\Server\RpcRequestHandler;
use Amp\Serialization\NativeSerializer;
use Amp\Socket;
use Monolog\Logger;
use function Amp\async;
use function Amp\ByteStream\getStdout;
use function Amp\Future\await;
use function Amp\Rpc\Examples\createRegistry;
use function Amp\trapSignal;

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('server');
$logger->pushHandler($logHandler);

$registry = new RpcRegistry();
$registry->register(TimeService::class, new class($id) implements TimeService {
public function getCurrentTime(): float
{
return now();
}
});

$serializer = new NativeSerializer;

$server = SocketHttpServer::createForDirectAccess($logger);
$server->expose('0.0.0.0:1337');
$server->expose('[::]:1337');
$server->start(new RpcRequestHandler($serializer, $registry), new DefaultErrorHandler());

// Stop the server when SIGINT is received (this is technically optional, but it is best to call Server::stop()).
trapSignal(\SIGINT);

$server->stop();
```

### Client

```php
<?php declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Amp\Rpc\Client\RpcClient;
use Amp\Rpc\Examples\Basic\TimeService;
use Amp\Rpc\Interceptor\RoundRobinBalancer;
use Amp\Serialization\NativeSerializer;
use Monolog\Logger;
use ProxyManager\Factory\RemoteObjectFactory;
use function Amp\ByteStream\getStdout;

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('client');
$logger->pushHandler($logHandler);

$serializer = new NativeSerializer;

$proxyFactory = new RemoteObjectFactory(new RoundRobinBalancer([
new RpcClient('http://localhost:1337/', $serializer),
]));

/** @var TimeService $timeService */
$timeService = $proxyFactory->createProxy(TimeService::class);

// This is a remote call via HTTP
var_dump($timeService->getCurrentTime());
```

## Security

If you discover any security related issues, please email [`contact@amphp.org`](mailto:contact@amphp.org) instead of using the issue tracker.
Expand Down
2 changes: 1 addition & 1 deletion examples/client.counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('server');
$logger = new Logger('client');
$logger->pushHandler($logHandler);

$context = (new ConnectContext)
Expand Down
2 changes: 1 addition & 1 deletion examples/client.time.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('server');
$logger = new Logger('client');
$logger->pushHandler($logHandler);

$serializer = new NativeSerializer;
Expand Down

0 comments on commit 1efa17e

Please sign in to comment.