예제 #1
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);
    }
});
예제 #2
0
 /**
  * Asynchronously removes a message from the channel.
  */
 public function take()
 {
     Redshift::async(function ($channel) {
         (yield $channel->read());
     }, $this);
 }
예제 #3
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));
});
예제 #4
0
/**
 * Alias for Redshift::async().
 * Schedules the $callback for asynchronous execution.
 *
 * @param callable $callback
 * @param mixed ...$args Arguments to be supplied to the $callback
 *                       at the time of execution.
 */
function async(callable $callback, ...$args)
{
    Redshift::async($callback, ...$args);
}
예제 #5
0
 /**
  * Returns a channel on which a signal will be written after
  * the specified time.
  *
  * @param int $milliseconds
  * @return Channel
  */
 public static function after($milliseconds)
 {
     $channel = new Channel();
     Redshift::async(new Timeout($milliseconds, $channel));
     return $channel;
 }
예제 #6
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;
});