I'm trying to create async server in hacklang. File name is first.php:
<?hh
namespace MyExperiment;
async function server(string $host, int $port): Awaitable<void> {
$master = stream_socket_server("tcp://$host:$port");
stream_set_blocking($master, 0);
while (true) {
await stream_await($master, STREAM_AWAIT_READ, 1.0);
$clientSocket = stream_socket_accept($master);
stream_set_blocking($clientSocket, 0);
handleClient($clientSocket);
}
}
async function handleClient($socket): Awaitable<void> {
await stream_await($socket, STREAM_AWAIT_READ, 1.0);
$data = fread($socket, 1024);
echo $data;
await stream_await($socket, STREAM_AWAIT_WRITE, 1.0);
fwrite($socket, 'aaaaaaaa');
fclose($socket);
}
function run(): void {
\HH\Asio\join(server('192.168.0.97', 8080));
}
run();
But this does not work. hh_client on this code says:
first.php:16:3,29: This expression is of type Awaitable, but it's either being discarded or used in a dangerous way before being awaited (Typing[4015])
first.php:20:39,47: This is why I think it is Awaitable
But I don't want to block and wait for handleClient.
Then I run code this way: hhvm -d hhvm.hack.lang.auto_typecheck=0 first.php
Server starts. But when I start to send requests to server http://192.168.0.97:8080/ from browser server blocks after few requests for very long time and does not accept new connections anymore.
Do I do something wrong? Is it possible to create server like this in hacklang at all?
$ hhvm --version
HipHop VM 3.11.0 (rel)
Compiler: tags/HHVM-3.11.0-0-g3dd564a8cde23e3205a29720d3435c771274085e
Repo schema: 52047bdda550f21c2ec2fcc295e0e6d02407be51