forked from revoltphp/event-loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-server.php
40 lines (32 loc) · 1.13 KB
/
http-server.php
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
39
40
<?php
declare(strict_types=1);
use Revolt\EventLoop;
require __DIR__ . '/../vendor/autoload.php';
// start TCP/IP server on localhost:8080
// for illustration purposes only, should use socket abstracting instead
$server = \stream_socket_server('tcp://0.0.0.0:8080');
if (!$server) {
exit(1);
}
\stream_set_blocking($server, false);
echo "Visit http://localhost:8080/ in your browser." . PHP_EOL;
// wait for incoming connections on server socket
EventLoop::onReadable($server, function ($watcher, $server) {
$conn = \stream_socket_accept($server);
$data = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 3\r\n\r\nHi\n";
EventLoop::onWritable($conn, function ($watcher, $conn) use (&$data) {
$written = \fwrite($conn, $data);
if ($written === \strlen($data)) {
\fclose($conn);
EventLoop::cancel($watcher);
} else {
$data = \substr($data, $written);
}
});
});
EventLoop::repeat(5, function () {
$memory = \memory_get_usage() / 1024;
$formatted = \number_format($memory).' KiB';
echo "Current memory usage: {$formatted}\n";
});
EventLoop::run();