39 lines
		
	
	
		
			966 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			966 B
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?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";
 | 
						|
} |