|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use React\EventLoop\Loop; |
| 4 | + |
3 | 5 | require_once __DIR__.'/../vendor/autoload.php'; |
4 | 6 |
|
5 | | -$loop = React\EventLoop\Factory::create(); |
6 | | -$factory = new React\Datagram\Factory($loop); |
| 7 | +$factory = new React\Datagram\Factory(); |
7 | 8 |
|
8 | | -$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) use ($loop) { |
| 9 | +$factory->createClient('localhost:1234')->then(function (React\Datagram\Socket $client) { |
9 | 10 | $client->send('first'); |
10 | 11 |
|
11 | 12 | $client->on('message', function($message, $serverAddress, $client) { |
|
17 | 18 | }); |
18 | 19 |
|
19 | 20 | $n = 0; |
20 | | - $tid = $loop->addPeriodicTimer(2.0, function() use ($client, &$n) { |
| 21 | + $tid = Loop::addPeriodicTimer(2.0, function() use ($client, &$n) { |
21 | 22 | $client->send('tick' . ++$n); |
22 | 23 | }); |
23 | 24 |
|
24 | 25 | // read input from STDIN and forward everything to server |
25 | | - $loop->addReadStream(STDIN, function () use ($client, $loop, $tid) { |
| 26 | + Loop::addReadStream(STDIN, function () use ($client, $tid) { |
26 | 27 | $msg = fgets(STDIN, 2000); |
27 | 28 | if ($msg === false) { |
28 | 29 | // EOF => flush client and stop perodic sending and waiting for input |
29 | 30 | $client->end(); |
30 | | - $loop->cancelTimer($tid); |
31 | | - $loop->removeReadStream(STDIN); |
| 31 | + Loop::cancelTimer($tid); |
| 32 | + Loop::removeReadStream(STDIN); |
32 | 33 | } else { |
33 | 34 | $client->send(trim($msg)); |
34 | 35 | } |
35 | 36 | }); |
36 | 37 | }, function($error) { |
37 | 38 | echo 'ERROR: ' . $error->getMessage() . PHP_EOL; |
38 | 39 | }); |
39 | | - |
40 | | -$loop->run(); |
0 commit comments