-
-
Notifications
You must be signed in to change notification settings - Fork 16
Closed
Labels
Description
I am trying to use reactphp in my application without rewriting the whole thing to promises but to use await instead. I have though some problems with await which I can boil down to this example:
<?php
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\Browser;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;
use function Clue\React\Block\await;
require 'vendor/autoload.php';
$http = new HttpServer(function (ServerRequestInterface $request) {
echo $request->getMethod() . ' ' . $request->getUri() . PHP_EOL;
return new Response(
200,
array(
'Content-Type' => 'text/plain'
),
"Hello World!\n"
);
});
$socket = new SocketServer('127.0.0.1:8080');
$http->listen($socket);
$txn = 1;
$preventInfiniteLoop = false;
$loop = Loop::get();
$loop->addPeriodicTimer(1, function() use(&$txn, &$preventInfiniteLoop) {
$browser = new Browser();
echo "$txn Timer running" . PHP_EOL;
// Synkron version
if ($preventInfiniteLoop) {
return;
}
$preventInfiniteLoop = true;
$response = await($browser->get('http://localhost:8080'));
echo $response->getBody() . PHP_EOL;
// Asyncron version
// $browser->get('http://localhost:8080')->then(function(ResponseInterface $response) {
// echo $response->getBody() . PHP_EOL;
// }, function(Exception $error) {
// var_dump($error);
// });
$preventInfiniteLoop = false;
$txn++;
});
echo "Server running at http://127.0.0.1:8080" . PHP_EOL;
Then asyncron works out of the box, but the syncron way has 2 issue:
- it goes into infinite loop
- i can prevent the infinite loop, but then it stops the server.