Skip to content

Commit 698d4de

Browse files
committed
Fix timers and improve HTTP examples
1 parent bf064ae commit 698d4de

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

examples/http-client-async.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414

1515
// establish TCP/IP connection (non-blocking)
16-
// for illustraction purposes only, should use a proper socket abstraction instead!
16+
// for illustration purposes only, should use a proper socket abstraction instead!
1717
$stream = \stream_socket_client('tcp://' . $ip . ':80', $errno, $errstr, PHP_INT_MAX, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);
1818
if (!$stream) {
1919
exit(1);
@@ -22,7 +22,7 @@
2222

2323
// print progress every 10ms
2424
echo 'Connecting';
25-
$timer = EventLoop::repeat(10, function () {
25+
$timer = EventLoop::repeat(0.01, function () {
2626
echo '.';
2727
});
2828

@@ -48,7 +48,7 @@
4848
return;
4949
}
5050

51-
echo $chunk;
51+
echo "Read " . \strlen($chunk) . " bytes..." . PHP_EOL;
5252
});
5353
});
5454

examples/http-client-blocking.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
return;
2828
}
2929

30-
echo $chunk;
30+
echo "Read " . \strlen($chunk) . " bytes..." . PHP_EOL;
3131
});
3232

3333
EventLoop::run();

examples/http-client-suspension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ function fetch(string $url): string
5454
return $buffer;
5555
}
5656

57-
echo fetch('http://www.google.com/');
57+
function extractHeader(string $httpResponse): string {
58+
return explode("\r\n\r\n", $httpResponse, 2)[0];
59+
}
60+
61+
echo extractHeader(fetch('http://www.google.com/'));

examples/http-server.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
}
1313
\stream_set_blocking($server, false);
1414

15+
echo "Visit http://localhost:8080/ in your browser." . PHP_EOL;
16+
1517
// wait for incoming connections on server socket
1618
EventLoop::onReadable($server, function ($watcher, $server) {
1719
$conn = \stream_socket_accept($server);
@@ -27,9 +29,9 @@
2729
});
2830
});
2931

30-
EventLoop::repeat(5000, function () {
32+
EventLoop::repeat(5, function () {
3133
$memory = \memory_get_usage() / 1024;
32-
$formatted = \number_format($memory, 3).'K';
34+
$formatted = \number_format($memory).' KiB';
3335
echo "Current memory usage: {$formatted}\n";
3436
});
3537

0 commit comments

Comments
 (0)