Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactPHP run every loop twice

Tags:

reactphp

I'm trying to learn now technology - reactPHP. But I'm stacked with starting script. I'd edited it little bit, but I have problem, if I call the react loop, the script is done twice.

I have this code:

<?php
require 'vendor/autoload.php';

$app = function ($request, $response) {
    $date = new DateTime();

    file_put_contents("data.txt", $date->getTimestamp().";", FILE_APPEND);

    $response->writeHead(200, array('Content-Type' => 'text/plain'));
    $response->end("Done\n");
};

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket, $loop);

$http->on('request', $app);
echo "Server running at http://127.0.0.1:1337\n";

$socket->listen(1337);
$loop->run();

and if I call http://localhost:1337/react/index.php I get in data.txt this

1439849018;1439849018;

I'm expecting only one value.

like image 291
Karel Poláček Avatar asked Aug 17 '15 22:08

Karel Poláček


1 Answers

I have tested your code and the problem is because your'e testing it in your browser. Your browser send request and then ask for favicon. That's it. On the image from Inspect it's the first and third line. Next time try to run the scripts from cmd.

It's the first and third line.

like image 55
Holicz Avatar answered Oct 05 '22 21:10

Holicz