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 if ($clientId !== $myClientId) { $stdio->overwrite(sprintf("[%s] %s\n", $clientId, $message->content)); } $channel->ack($message); }, $myClientId); });
<?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();
<?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();
<?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 ;
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())); }