예제 #1
0
use CrystalPlanet\Redshift\Channel\Channel;
function fibonacci($c, $quit)
{
    $x = 0;
    $y = 1;
    while (true) {
        list($value, $channel) = (yield Channel::any([$c, $x], $quit));
        switch ($channel) {
            case $c:
                $tmp = $x + $y;
                $x = $y;
                $y = $tmp;
                break;
            case $quit:
                echo $value;
                return;
        }
    }
}
Redshift::run(function () {
    $c = new Channel();
    $quit = new Channel();
    async(function ($c, $quit) {
        for ($i = 0; $i < 10; ++$i) {
            $n = (yield $c->read());
            echo "{$n}\n";
        }
        (yield $quit->write("Quit\n"));
    }, $c, $quit);
    (yield fibonacci($c, $quit));
});
예제 #2
0
<?php

require '../../vendor/autoload.php';
use CrystalPlanet\Redshift\Redshift;
use CrystalPlanet\Redshift\Stream\Stream;
Redshift::run(function () {
    $stream = stream_socket_server('tcp://0.0.0.0:9000');
    stream_set_blocking($stream, 0);
    $server = new Stream($stream);
    while (true) {
        $client = (yield $server->executeAsyncRead('stream_socket_accept'));
        async(function ($client) {
            $data = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello World!\n";
            fwrite($client, $data);
            fclose($client);
        }, $client);
    }
});
예제 #3
0
<?php

require_once '../../vendor/autoload.php';
use CrystalPlanet\Redshift\Redshift;
use CrystalPlanet\Redshift\Channel\Channel;
Redshift::run(function () {
    $channel = new Channel();
    // Write 'foo' to a channel
    (yield $channel->write('foo'));
    // Read 'foo' from a channel
    $message = (yield $channel->read());
    // Print 'foo'
    echo $message . PHP_EOL;
});