Added MultiStreamChannel to handle multiple endpoints
This commit is contained in:
39
examples/multichannels.php
Normal file
39
examples/multichannels.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
require_once __DIR__."/../vendor/autoload.php";
|
||||
|
||||
use NoccyLabs\Ipc\Interop\Channel\MultiStreamChannel;
|
||||
|
||||
// It really is this simple
|
||||
$channel = new MultiStreamChannel();
|
||||
|
||||
// Create a child to report back in 3 seconds
|
||||
$channel1 = $channel->createClient();
|
||||
$pid1 = pcntl_fork();
|
||||
if ($pid1 === 0) {
|
||||
sleep(1);
|
||||
echo "child 1 received ".$channel1->receive()."\n";
|
||||
$channel1->send("Hello from child 1");
|
||||
sleep(5);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Create another child to report back in 4 seconds
|
||||
$channel2 = $channel->createClient();
|
||||
$pid2 = pcntl_fork();
|
||||
if ($pid2 === 0) {
|
||||
sleep(1);
|
||||
echo "child 2 received ".$channel2->receive()."\n";
|
||||
$channel2->send("Hello from child 2");
|
||||
sleep(5);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Writing to the master works whether any clients have been added
|
||||
$channel->send("Hello from master");
|
||||
|
||||
// Wait for 5 seconds and dump whatever messages are waiting
|
||||
sleep(5);
|
||||
while ($msg = $channel->receive()) {
|
||||
echo "master received ".$msg."\n";
|
||||
}
|
Reference in New Issue
Block a user