xref: /php-uv/examples/fingerd.php (revision 99312394)
1<?php
2$tcp = uv_tcp_init();
3
4uv_tcp_bind($tcp, uv_ip4_addr('0.0.0.0',79));
5$users = array(
6    array(
7        "username" => "chobie",
8        "name"     => "Shuhei Tanuma",
9        "twitter"  => "chobi_e",
10    ),
11);
12
13function pad($str)
14{
15    return str_pad($str, 20, ' ',STR_PAD_RIGHT);
16}
17
18uv_listen($tcp, 100, function($server) use ($users) {
19    $client = uv_tcp_init();
20    uv_accept($server, $client);
21    uv_read_start($client, function($socket, $buffer) use ($users){
22        $buffer = str_replace("/W","",$buffer);
23        if ($buffer == "\r\n") {
24            $data = "";
25            $keys = array("Login","Name","Twitter");
26            $data .= join("",array_map("pad",$keys)) . "\r\n";
27            foreach($users as $user) {
28                $data .= join("", array_map("pad",array_values($user))) . "\r\n";
29            }
30
31            uv_write($socket, $data, function($client, $stat) {
32                uv_close($client);
33            });
34        } else {
35            var_dump($buffer);
36            uv_close($socket);
37        }
38    });
39});
40
41uv_run();
42