File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -1426,15 +1426,23 @@ may only support strings.
1426
1426
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
1427
1427
$stream = new ThroughStream();
1428
1428
1429
+ // send some data every once in a while with periodic timer
1429
1430
$timer = Loop::addPeriodicTimer(0.5, function () use ($stream) {
1430
1431
$stream->write(microtime(true) . PHP_EOL);
1431
1432
});
1432
1433
1433
- Loop::addTimer(5, function() use ($timer, $stream) {
1434
+ // end stream after a few seconds
1435
+ $timeout = Loop::addTimer(5.0, function() use ($stream, $timer) {
1434
1436
Loop::cancelTimer($timer);
1435
1437
$stream->end();
1436
1438
});
1437
1439
1440
+ // stop timer if stream is closed (such as when connection is closed)
1441
+ $stream->on('close', function () use ($timer, $timeout) {
1442
+ Loop::cancelTimer($timer);
1443
+ Loop::cancelTimer($timeout);
1444
+ });
1445
+
1438
1446
return new React\Http\Message\Response(
1439
1447
React\Http\Message\Response::STATUS_OK,
1440
1448
array(
Original file line number Diff line number Diff line change 18
18
$ stream ->write (microtime (true ) . PHP_EOL );
19
19
});
20
20
21
- // demo for ending stream after a few seconds
22
- Loop::addTimer (5.0 , function () use ($ stream ) {
21
+ // end stream after a few seconds
22
+ $ timeout = Loop::addTimer (5.0 , function () use ($ stream , $ timer ) {
23
+ Loop::cancelTimer ($ timer );
23
24
$ stream ->end ();
24
25
});
25
26
26
27
// stop timer if stream is closed (such as when connection is closed)
27
- $ stream ->on ('close ' , function () use ($ timer ) {
28
+ $ stream ->on ('close ' , function () use ($ timer, $ timeout ) {
28
29
Loop::cancelTimer ($ timer );
30
+ Loop::cancelTimer ($ timeout );
29
31
});
30
32
31
33
return new Response (
You can’t perform that action at this time.
0 commit comments