Skip to content

Commit ba9adaa

Browse files
committed
Demo Socket::end() in example
1 parent cde9533 commit ba9adaa

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

example/client.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@
2121
});
2222

2323
$n = 0;
24-
$loop->addPeriodicTimer(2.0, function() use ($client, &$n) {
24+
$tid = $loop->addPeriodicTimer(2.0, function() use ($client, &$n) {
2525
$client->send('tick' . ++$n);
2626
});
2727

2828
// read input from STDIN and forward everything to server
29-
$loop->addReadStream(STDIN, function () use ($client) {
30-
$client->send(trim(fgets(STDIN, 2000)));
29+
$loop->addReadStream(STDIN, function () use ($client, $loop, $tid) {
30+
$msg = fgets(STDIN, 2000);
31+
if ($msg === false) {
32+
// EOF => flush client and stop perodic sending and waiting for input
33+
$client->end();
34+
$loop->cancelTimer($tid);
35+
$loop->removeReadStream(STDIN);
36+
} else {
37+
$client->send(trim($msg));
38+
}
3139
});
3240
}, function($error) {
3341
echo 'ERROR: ' . $error->getMessage() . PHP_EOL;

0 commit comments

Comments
 (0)