1
0
mirror of https://github.com/danog/ipc.git synced 2024-12-02 09:27:46 +01:00
ipc/README.md
2020-03-05 21:12:35 +01:00

1.8 KiB

template

Build Status CoverageStatus License

amphp/ipc provides an async IPC server.

Installation

composer require amphp/ipc

Example

Server:

<?php

require 'vendor/autoload.php';

use Amp\Ipc\IpcServer;
use Amp\Loop;
use Amp\Ipc\Sync\ChannelledSocket;

use function Amp\asyncCall;

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');
            }
        }
        yield $socket->disconnect();
        echo "Closed connection".PHP_EOL;
    };

    $server = new IpcServer(sys_get_temp_dir().'/test');
    while ($socket = yield $server->accept()) {
        asyncCall($clientHandler, $socket);
    }
});

Client:

<?php

require 'vendor/autoload.php';

use Amp\Loop;
use Amp\Ipc\Sync\ChannelledSocket;

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;
            yield $socket->disconnect();
        }
        echo "Closed connection".PHP_EOL;
    };

    $channel = yield connect(sys_get_temp_dir().'/test');
    asyncCall($clientHandler, $channel);
    yield $channel->send('ping');
});