예제 #1
0
function on_read($event)
{
    $read = "on_read: ";
    bufferevent_read($event, $read, 100);
    bufferevent_write($event, $read);
}
예제 #2
0
function on_accept($stream, $flags, $event)
{
    global $CLIENTS;
    global $count;
    $count++;
    $nick = "guest{$count}";
    $time = date('H:i:s');
    $mem = memory_get_usage();
    //echo "on_accept($stream, $flags): mem=$mem \n";
    if (false === ($con = stream_socket_accept($stream, 0, $ip))) {
        echo "Accept failed\n";
    } else {
        // notify server
        echo "Connection from: {$ip} ({$nick})\n";
        $client = bufferevent_new($con, 'on_read', 'on_write', 'on_error');
        //debug_zval_dump($client);
        // TODO: If bufferevent falls out of scope: disable and free buffer. also close stream.
        // alert all other clients of new connection
        foreach ($CLIENTS as $c) {
            bufferevent_write($c['client'], $time . " {$nick} has entered SamChat.\n");
        }
        $CLIENTS[$ip] = array('client' => &$client, 'con' => &$con, 'nick' => $nick, 'admin' => false);
        bufferevent_enable($client, EV_READ);
        bufferevent_write($client, "Welcome to SamChat {$nick}! Type '/HELP' for help.\n");
    }
}