-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use timeouts to avoid long buffering in CompressionMiddleware (#334)
Closes #324.
- Loading branch information
Showing
3 changed files
with
138 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
require \dirname(__DIR__) . "/vendor/autoload.php"; | ||
|
||
use Amp\ByteStream\IteratorStream; | ||
use Amp\ByteStream\ResourceOutputStream; | ||
use Amp\Delayed; | ||
use Amp\Http\Server\HttpServer; | ||
use Amp\Http\Server\Request; | ||
use Amp\Http\Server\RequestHandler\CallableRequestHandler; | ||
use Amp\Http\Server\Response; | ||
use Amp\Http\Status; | ||
use Amp\Log\ConsoleFormatter; | ||
use Amp\Log\StreamHandler; | ||
use Amp\Producer; | ||
use Amp\Socket; | ||
use Monolog\Logger; | ||
|
||
// Run this script, then visit http://localhost:1337/ in your browser. | ||
|
||
$html = <<<HTML | ||
<html lang="en"> | ||
<head> | ||
<title>Event Source Demo</title> | ||
</head> | ||
<body> | ||
<script> | ||
const eventSource = new EventSource('/events'); | ||
const eventList = document.createElement('ol'); | ||
document.body.appendChild(eventList); | ||
eventSource.addEventListener('notification', function (e) { | ||
const element = document.createElement('li'); | ||
element.textContent = 'Message: ' + e.data; | ||
eventList.appendChild(element); | ||
}); | ||
</script> | ||
</body> | ||
</html> | ||
HTML; | ||
|
||
Amp\Loop::run(function () use ($html) { | ||
$servers = [ | ||
Socket\Server::listen("0.0.0.0:1337"), | ||
Socket\Server::listen("[::]:1337"), | ||
]; | ||
|
||
$logHandler = new StreamHandler(new ResourceOutputStream(\STDOUT)); | ||
$logHandler->setFormatter(new ConsoleFormatter); | ||
$logger = new Logger('server'); | ||
$logger->pushHandler($logHandler); | ||
|
||
$server = new HttpServer($servers, new CallableRequestHandler(function (Request $request) use ($html) { | ||
$path = $request->getUri()->getPath(); | ||
|
||
if ($path === '/') { | ||
return new Response(Status::OK, [ | ||
"content-type" => "text/html; charset=utf-8", | ||
], $html); | ||
} | ||
|
||
if ($path === '/events') { | ||
// We stream the response here, one event every 500 ms. | ||
return new Response(Status::OK, [ | ||
"content-type" => "text/event-stream; charset=utf-8", | ||
], new IteratorStream(new Producer(function (callable $emit) { | ||
for ($i = 0; $i < 30; $i++) { | ||
yield new Delayed(500); | ||
yield $emit("event: notification\ndata: Event {$i}\n\n"); | ||
} | ||
}))); | ||
} | ||
|
||
return new Response(Status::NOT_FOUND); | ||
}), $logger); | ||
|
||
yield $server->start(); | ||
|
||
// Stop the server when SIGINT is received (this is technically optional, but it is best to call Server::stop()). | ||
Amp\Loop::onSignal(SIGINT, function (string $watcherId) use ($server) { | ||
Amp\Loop::cancel($watcherId); | ||
yield $server->stop(); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters