forked from clue/reactphp-stdio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-periodic.php
More file actions
38 lines (27 loc) · 979 Bytes
/
01-periodic.php
File metadata and controls
38 lines (27 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
use Clue\React\Stdio\Stdio;
require __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$stdio = new Stdio($loop);
$stdio->write('Will print periodic messages until you submit anything' . PHP_EOL);
// add some periodic noise
$timer = $loop->addPeriodicTimer(0.5, function () use ($stdio) {
$stdio->write(date('Y-m-d H:i:s') . ' hello' . PHP_EOL);
});
// react to commands the user entered
$stdio->on('data', function ($line) use ($stdio, $loop, $timer) {
$stdio->write('you just said: ' . addcslashes($line, "\0..\37") . ' (' . strlen($line) . ')' . PHP_EOL);
$loop->cancelTimer($timer);
$stdio->end();
});
// cancel periodic timer if STDIN closed
$stdio->on('end', function () use ($stdio, $loop, $timer) {
$loop->cancelTimer($timer);
$stdio->end();
});
// input already closed on program start, exit immediately
if (!$stdio->isReadable()) {
$loop->cancelTimer($timer);
$stdio->end();
}
$loop->run();