Skip to content

Commit

Permalink
Revert example changes
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Nov 13, 2024
1 parent 9b89e3d commit 8277728
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions examples/event-source.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
$server->expose("0.0.0.0:1337");
$server->expose("[::]:1337");

$server->start(new ClosureRequestHandler(function (Request $request) use ($html, $logger): Response {
$server->start(new ClosureRequestHandler(function (Request $request) use ($html): Response {
$path = $request->getUri()->getPath();

if ($path === '/') {
Expand All @@ -63,28 +63,17 @@
}

if ($path === '/events') {
$body = new ReadableIterableStream((function () use ($logger) {
$i = 0;
try {
// We stream the response here, one event every 500 ms.
return new Response(
status: HttpStatus::OK,
headers: ["content-type" => "text/event-stream; charset=utf-8"],
body: new ReadableIterableStream((function () {
for ($i = 0; $i < 30; $i++) {
delay(0.5);
yield "event: notification\ndata: Event {$i}\n\n";
}
} finally {
$logger->info('Response closed at ' . $i);
}
})());

// We stream the response here, one event every 500 ms.
$response = new Response(
status: HttpStatus::OK,
headers: ["content-type" => "text/event-stream; charset=utf-8"],
body: $body,
})()),
);

$response->onDispose(static fn () => $body->close());

return $response;
}

return new Response(HttpStatus::NOT_FOUND);
Expand Down

0 comments on commit 8277728

Please sign in to comment.