Esempio n. 1
0
<?php

use Clue\React\Stdio\Stdio;
use Clue\React\Stdio\Helper\ProgressBar;
require __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$stdio = new Stdio($loop);
$stdio->writeln('Will print (fake) progress and then exit');
$progress = new ProgressBar($stdio);
$progress->setMaximum(mt_rand(20, 200));
$loop->addPeriodicTimer(0.1, function ($timer) use($stdio, $progress) {
    $progress->advance();
    if ($progress->isComplete()) {
        $stdio->overwrite("Finished processing nothing!" . PHP_EOL);
        $stdio->end();
        $timer->cancel();
    }
});
$loop->run();
Esempio n. 2
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use Bunny\Async\Client;
use Bunny\Channel;
use Bunny\Message;
use Clue\React\Stdio\Stdio;
use React\EventLoop\Factory;
use React\Promise;
$loop = Factory::create();
$myClientId = $argv[1];
// use this brutally from CLI params, it's just a demo
$stdio = new Stdio($loop);
(new Client($loop, ['host' => '192.168.33.99']))->connect()->then(function (Client $client) {
    return $client->channel();
})->then(function (Channel $channel) use($stdio, $loop, $myClientId) {
    // Set up I/O
    $stdio->getReadline()->setPrompt(sprintf('[%s] ', $myClientId));
    $stdio->on('line', function ($line) use($loop, $channel, $myClientId) {
        if ($line === 'quit') {
            $loop->stop();
        }
        $channel->publish($line, ['chatClientId' => $myClientId], 'bunny_chat_exchange');
    });
    return Promise\all([$channel, $channel->queueDeclare($myClientId, false, false, true, true), $channel->exchangeDeclare('bunny_chat_exchange', 'fanout'), $channel->queueBind($myClientId, 'bunny_chat_exchange')]);
})->then(function (array $r) use($stdio, $myClientId) {
    /** @var Channel $channel */
    $channel = $r[0];
    return $channel->consume(function (Message $message, Channel $channel, Client $client) use($stdio, $myClientId) {
        $clientId = $message->getHeader('chatClientId');
        // No local echo
Esempio n. 3
0
<?php

use Clue\React\Stdio\Stdio;
require __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$stdio = new Stdio($loop);
$stdio->getReadline()->setPrompt('> ');
$stdio->writeln('Will print periodic messages until you type "quit" or "exit"');
$stdio->on('line', function ($line) use($stdio, $loop, &$timer) {
    $stdio->writeln('you just said: ' . $line . ' (' . strlen($line) . ')');
    if ($line === 'quit' || $line === 'exit') {
        $timer->cancel();
        $stdio->end();
    }
});
// add some periodic noise
$timer = $loop->addPeriodicTimer(2.0, function () use($stdio) {
    $stdio->writeln('hello');
});
$loop->run();
Esempio n. 4
0
<?php

use Clue\React\Stdio\Stdio;
require __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$stdio = new Stdio($loop);
$stdio->getReadline()->setPrompt('Username: '******'line', function ($line) use($stdio, &$first, &$username, &$password) {
    if ($first) {
        $stdio->getReadline()->setPrompt('Password: '******'*');
        $username = $line;
        $first = false;
    } else {
        $password = $line;
        $stdio->end();
    }
});
$loop->run();
echo <<<EOT
---------------------
Confirmation:
---------------------
Username: {$username}
Password: {$password}

EOT
;
Esempio n. 5
0
<?php

use Clue\React\Stdio\Stdio;
use React\EventLoop\LoopInterface;
use Clue\React\Stdio\Helper\Spinner;
require __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$stdio = new Stdio($loop);
$stdio->getReadline()->setPrompt('> ');
$stdio->writeln('Will print a spinner until you enter something');
$stdio->write('  Processing...');
$spinner = new Spinner($loop, $stdio);
$stdio->on('line', function ($line) use($stdio, &$tid, $loop, $spinner) {
    $stdio->overwrite('Processing... DONE');
    $stdio->end();
    $spinner->pause();
});
$loop->run();
Esempio n. 6
0
 public function testErrorEventFromOutputWillBeForwarded()
 {
     $input = $this->getMock('React\\Stream\\ReadableStreamInterface');
     $output = new WritableStream();
     //$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
     $readline = new Readline($input, $output);
     $stdio = new Stdio($this->loop, $input, $output, $readline);
     $stdio->on('error', $this->expectCallableOnce());
     $output->emit('error', array(new \RuntimeException()));
 }