Skip to content

Commit

Permalink
Adopted examples to work with the most current version of react/http
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-kluge committed Mar 3, 2019
1 parent a2d08b7 commit c81c3cc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
14 changes: 4 additions & 10 deletions examples/01-server-with-compression.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Io\MiddlewareRunner;
use React\Http\Response;
use React\Http\Server;
use Sikei\React\Http\Middleware\ResponseCompressionMiddleware;
Expand All @@ -13,27 +12,22 @@

$loop = Factory::create();

$server = new Server(new MiddlewareRunner([
$server = new Server([
new ResponseCompressionMiddleware([
new CompressionGzipHandler(),
new CompressionDeflateHandler(),
]),
function (ServerRequestInterface $request, callable $next) {
function (ServerRequestInterface $request) {
return new Response(200, ['Content-Type' => 'application/json'], json_encode([
'some' => 'nice',
'json' => 'values',
]));
},
]));
}
]);

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$server->listen($socket);

$server->on('error', function(Exception $error) {
echo get_class($error) . ' - ' . $error->getMessage() . PHP_EOL;
echo get_class($error->getPrevious()) . ' - ' . $error->getPrevious()->getMessage() . PHP_EOL;
});

echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;

$loop->run();
7 changes: 3 additions & 4 deletions examples/02-server-with-compression-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Io\MiddlewareRunner;
use React\Http\Response;
use React\Http\Server;
use React\Stream\ThroughStream;
Expand All @@ -14,12 +13,12 @@

$loop = Factory::create();

$server = new Server(new MiddlewareRunner([
$server = new Server([
new ResponseCompressionMiddleware([
new CompressionGzipHandler(),
new CompressionDeflateHandler(),
]),
function (ServerRequestInterface $request, callable $next) use ($loop) {
function (ServerRequestInterface $request) use ($loop) {
$stream = new ThroughStream();
$loop->addTimer(0.001, function () use ($stream) {
$stream->write('{');
Expand All @@ -34,7 +33,7 @@ function (ServerRequestInterface $request, callable $next) use ($loop) {

return new Response(200, ['Content-Type' => 'application/json'], $stream);
},
]));
]);

$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$server->listen($socket);
Expand Down

0 comments on commit c81c3cc

Please sign in to comment.