1
0
mirror of https://github.com/danog/ipc.git synced 2024-11-30 04:29:09 +01:00
ipc/README.md

81 lines
1.8 KiB
Markdown
Raw Normal View History

2020-03-05 23:46:05 +01:00
# IPC
2020-02-14 20:31:11 +01:00
2020-03-05 21:13:39 +01:00
[![Build Status](https://img.shields.io/travis/danog/ipc/master.svg?style=flat-square)](https://travis-ci.org/danog/ipc)
2020-03-05 21:59:20 +01:00
[![Build status](https://ci.appveyor.com/api/projects/status/1tcxa257p5dj52ck?svg=true)](https://ci.appveyor.com/project/danog/ipc)
2020-02-14 20:31:11 +01:00
![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)
2020-03-05 23:46:05 +01:00
`danog/ipc` provides an async IPC server.
2020-02-14 20:31:11 +01:00
## Installation
```bash
2020-03-05 23:46:05 +01:00
composer require danog/ipc
2020-02-14 20:31:11 +01:00
```
## Example
2020-03-05 21:12:35 +01:00
Server:
2020-02-14 20:31:11 +01:00
```php
<?php
require 'vendor/autoload.php';
2020-03-05 21:12:35 +01:00
use Amp\Ipc\Sync\ChannelledSocket;
2020-03-05 21:59:20 +01:00
use Amp\Loop;
2020-03-05 21:12:35 +01:00
use function Amp\asyncCall;
2020-07-17 14:37:37 +02:00
use function Amp\Ipc\listen;
2020-03-05 21:12:35 +01:00
Loop::run(static function () {
$clientHandler = function (ChannelledSocket $socket) {
echo "Accepted connection".PHP_EOL;
while ($payload = yield $socket->receive()) {
echo "Received $payload".PHP_EOL;
if ($payload === 'ping') {
yield $socket->send('pong');
2020-03-05 21:59:20 +01:00
yield $socket->disconnect();
2020-03-05 21:12:35 +01:00
}
}
2020-03-05 21:59:20 +01:00
echo "Closed connection".PHP_EOL."==========".PHP_EOL;
2020-03-05 21:12:35 +01:00
};
2020-07-17 14:37:37 +02:00
$server = listen(\sys_get_temp_dir().'/test');
2020-03-05 21:12:35 +01:00
while ($socket = yield $server->accept()) {
asyncCall($clientHandler, $socket);
}
});
2020-03-05 21:59:20 +01:00
2020-02-14 20:31:11 +01:00
```
2020-03-05 21:12:35 +01:00
Client:
```php
<?php
require 'vendor/autoload.php';
use Amp\Ipc\Sync\ChannelledSocket;
2020-03-05 21:59:20 +01:00
use Amp\Loop;
2020-03-05 21:12:35 +01:00
use function Amp\asyncCall;
use function Amp\Ipc\connect;
Loop::run(static function () {
$clientHandler = function (ChannelledSocket $socket) {
echo "Created connection.".PHP_EOL;
while ($payload = yield $socket->receive()) {
echo "Received $payload".PHP_EOL;
}
echo "Closed connection".PHP_EOL;
};
2020-03-05 21:59:20 +01:00
$channel = yield connect(\sys_get_temp_dir().'/test');
2020-03-05 21:12:35 +01:00
asyncCall($clientHandler, $channel);
yield $channel->send('ping');
});
2020-03-05 23:46:05 +01:00
```
2021-01-30 20:51:30 +01:00