-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path01-periodic.php
More file actions
34 lines (25 loc) · 912 Bytes
/
01-periodic.php
File metadata and controls
34 lines (25 loc) · 912 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
<?php
use React\EventLoop\Loop;
require __DIR__ . '/../vendor/autoload.php';
$stdio = new Clue\React\Stdio\Stdio();
$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, $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, $timer) {
Loop::cancelTimer($timer);
$stdio->end();
});
// input already closed on program start, exit immediately
if (!$stdio->isReadable()) {
Loop::cancelTimer($timer);
$stdio->end();
}