@@ -593,12 +593,12 @@ $server = new Server(function (ServerRequestInterface $request) use ($loop) {
593593 $stream = new ThroughStream();
594594
595595 $timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
596- $stream->emit('data', array( microtime(true) . PHP_EOL) );
596+ $stream->write( microtime(true) . PHP_EOL);
597597 });
598598
599599 $loop->addTimer(5, function() use ($loop, $timer, $stream) {
600600 $loop->cancelTimer($timer);
601- $stream->emit(' end' );
601+ $stream->end( );
602602 });
603603
604604 return new Response(
@@ -675,25 +675,41 @@ in this case (if applicable).
675675
676676#### Response length
677677
678- If the response body is a ` string ` , a ` Content-Length ` header will be added
679- automatically.
678+ If the response body size is known, a ` Content-Length ` response header will be
679+ added automatically. This is the most common use case, for example when using
680+ a ` string ` response body like this:
680681
681- If the response body is a ReactPHP ` ReadableStreamInterface ` and you do not
682- specify a ` Content-Length ` header, outgoing HTTP/1.1 response messages will
683- automatically use ` Transfer-Encoding: chunked ` and send the respective header
684- automatically.
685- The server is responsible for handling ` Transfer-Encoding ` , so you SHOULD NOT
686- pass this header yourself.
682+ ``` php
683+ $server = new Server(function (ServerRequestInterface $request) {
684+ return new Response(
685+ 200,
686+ array(
687+ 'Content-Type' => 'text/plain'
688+ ),
689+ "Hello World!\n"
690+ );
691+ });
692+ ```
687693
688- If you know the length of your stream body, you MAY specify it like this instead:
694+ If the response body size is unknown, a ` Content-Length ` response header can not
695+ be added automatically. When using a [ streaming response] ( #streaming-response )
696+ without an explicit ` Content-Length ` response header, outgoing HTTP/1.1 response
697+ messages will automatically use ` Transfer-Encoding: chunked ` while legacy HTTP/1.0
698+ response messages will contain the plain response body. If you know the length
699+ of your streaming response body, you MAY want to specify it explicitly like this:
689700
690701``` php
691- $stream = new ThroughStream();
692- $server = new Server(function (ServerRequestInterface $request) use ($stream) {
702+ $server = new Server(function (ServerRequestInterface $request) use ($loop) {
703+ $stream = new ThroughStream();
704+
705+ $loop->addTimer(2.0, function () use ($stream) {
706+ $stream->end("Hello World!\n");
707+ });
708+
693709 return new Response(
694710 200,
695711 array(
696- 'Content-Length' => '5 ',
712+ 'Content-Length' => '13 ',
697713 'Content-Type' => 'text/plain',
698714 ),
699715 $stream
0 commit comments