Example #1
0
 public function onWeb(\React\Socket\Connection $incoming)
 {
     $slaveId = $this->getNextSlave();
     $port = $this->slaves[$slaveId]['port'];
     $client = stream_socket_client('tcp://127.0.0.1:' . $port);
     $redirect = new \React\Stream\Stream($client, $this->loop);
     $redirect->on('close', function () use($incoming) {
         $incoming->end();
     });
     $incoming->on('data', function ($data) use($redirect) {
         $redirect->write($data);
     });
     $redirect->on('data', function ($data) use($incoming) {
         $incoming->write($data);
     });
 }
Example #2
0
<?php

require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$superFab = new \Fab\SuperFab();
$read = new \React\Stream\Stream(fopen('php://stdin', 'r+'), $loop);
$write = new \React\Stream\Stream(fopen('php://stdout', 'w+'), $loop);
$read->on('data', function ($data, $read) use($write, $superFab) {
    if (trim($data) == 'quit') {
        $write->close();
        $read->close();
    }
    $input = trim($data);
    $line = Kitten::get() . ' says "' . $input . '"';
    $line = $superFab->paint($line);
    $line .= PHP_EOL;
    $write->write($line);
});
$loop->run();
<?php

require __DIR__ . '/../vendor/autoload.php';
$args = getopt('i:o:t:');
$if = isset($args['i']) ? $args['i'] : '/dev/zero';
$of = isset($args['o']) ? $args['o'] : '/dev/null';
$t = isset($args['t']) ? $args['t'] : 1;
// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
$if = str_replace('/dev/fd/', 'php://fd/', $if);
$of = str_replace('/dev/fd/', 'php://fd/', $of);
$loop = new React\EventLoop\StreamSelectLoop();
// setup information stream
$info = new React\Stream\Stream(STDERR, $loop);
$info->pause();
if (extension_loaded('xdebug')) {
    $info->write('NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL);
}
$info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...' . PHP_EOL);
// setup input and output streams and pipe inbetween
$in = new React\Stream\Stream(fopen($if, 'r'), $loop);
$out = new React\Stream\Stream(fopen($of, 'w'), $loop);
$out->pause();
$in->pipe($out);
// stop input stream in $t seconds
$start = microtime(true);
$timeout = $loop->addTimer($t, function () use($in, &$bytes) {
    $in->close();
});
// print stream position once stream closes
$in->on('close', function () use($in, $start, $timeout, $info) {
    $t = microtime(true) - $start;
Example #4
0
<?php

require 'vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$stream = new \React\Stream\Stream(fopen('php://stdout', 'w+'), $loop);
$i = 0;
$loop->addPeriodicTimer(1, function (React\EventLoop\Timer\Timer $timer) use(&$i, $loop, $stream) {
    $stream->write(++$i . PHP_EOL);
    if ($i >= 15) {
        $loop->cancelTimer($timer);
        $stream->end();
    }
});
$loop->run();