🐕
【PHP】Revolt のイベントループで HTTP/2 フレームを送受信する
Revolt のブロッキングなサンプルコードでうまくいったので記録
use Revolt\EventLoop;
require __DIR__ . '/vendor/autoload.php';
// https://github.com/revoltphp/event-loop/blob/358572ca31647df7dbe930f13c8567136beffa33/examples/http-client-blocking.php
// https://gist.github.com/masakielastic/924f750722ad06a25363735b48094fb2
require 'H2FrameIter.php';
require 'h2testdata.php';
$stream = stream_socket_client('tcp://localhost:8000');
stream_set_blocking($stream, false);
fwrite($stream, h2_client_frame());
$callbackId = EventLoop::onReadable($stream, function ($watcher, $stream) {
$chunk = fread($stream, 1024);
if ($chunk === '') {
EventLoop::cancel($watcher);
fclose($stream);
return;
}
$iter = H2FrameIter($chunk);
$current = 0;
foreach ($iter as $key => $next) {
var_dump([
'key' => $key,
'current' => $current,
'next' => $next
]);
if (isEndStream($chunk, $current)) {
var_dump(['payload' => getPayload($chunk, $current)]);
EventLoop::cancel($watcher);
fclose($stream);
return;
}
$current = $next;
}
});
EventLoop::run();
EventLoop::disable($callbackId);
Discussion