Commit b703b050 authored by Maxime Veber's avatar Maxime Veber
Browse files

Test select stream

parents
Pipeline #618 canceled with stages
{
"name": "veber/async-test",
"require": {
"php": ">=7.3",
"symfony/http-client": "dev-master",
"react/event-loop": "^1.1",
"react/promise": "^2.7",
"react/stream": "^1.1",
"react/promise-stream": "^1.2"
},
"authors": [
{
"name": "Maxime Veber",
"email": "nek.dev@gmail.com"
}
]
}
<?php
$vars = [];
if (!$_POST) {
$_POST = json_decode(file_get_contents('php://input'), true);
$_POST['content-type'] = $_SERVER['HTTP_CONTENT_TYPE'] ?? '?';
}
foreach ($_SERVER as $k => $v) {
switch ($k) {
default:
if (0 !== strpos($k, 'HTTP_')) {
continue 2;
}
// no break
case 'SERVER_NAME':
case 'SERVER_PROTOCOL':
case 'REQUEST_URI':
case 'REQUEST_METHOD':
case 'PHP_AUTH_USER':
case 'PHP_AUTH_PW':
$vars[$k] = $v;
}
}
$json = json_encode($vars, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
switch ($vars['REQUEST_URI']) {
case '/long-to-execute':
sleep(5);
break;
case '/faster':
sleep(1);
break;
}
header('Content-Type: application/json', true);
echo $json;
<?php
use Symfony\Component\HttpClient\NativeHttpClient;
require 'vendor/autoload.php';
$client = new NativeHttpClient();
$response1 = $client->request('GET', 'http://localhost:8000/long-to-execute');
$stream1 = $response1->toStream();
stream_set_blocking($stream1, 0);
$response2 = $response = $client->request('GET', 'http://localhost:8001/faster');
$stream2 = $response2->toStream();
stream_set_blocking($stream2, 0);
$write = [];
$except = [];
do {
$read = [$stream1, $stream2];
$foo = stream_select($read, $write, $except, null, 0);
foreach ($read as $item) {
var_dump(stream_get_contents($item));
}
echo "Stream 1: \n";
var_dump(\feof($stream1));
echo "Stream 2: \n";
var_dump(\feof($stream2));
} while (!\feof($stream1) && !\feof($stream2)); // while streams are not finished
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment