CORE COMPONENTS

  • EventLoop
  • Steam
  • Promise

EventLoop

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ด๋ฒคํŠธI/O์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•ต์‹ฌ ์ด๋ฒคํŠธ๋ฃจํ”„
๋น„๋™๊ธฐ ๊ธฐ๋ฐ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ƒํ˜ธ ์šด์šฉ ํ•  ์ˆ˜ ์žˆ์œผ๋ ค๋ฉด ๋™์ผํ•œ ์ด๋ฒคํŠธ ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ด ๊ตฌ์„ฑ ์š”์†Œ๋Š” ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋Œ€์ƒ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณตํ†ต LoobInterface๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์ œ์–ดํ•˜๋Š” ํ•˜๋‚˜์˜ ๋‹จ์ผ run()ํ˜ธ์ถœ๋กœ ๋™์ผํ•œ ๋ฃจํ”„์—์„œ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Quickstart example

์ด๋ฒคํŠธ ๋ฃจํ”„๋กœ ์ž‘์„ฑ๋œ ๋น„๋™๊ธฐ HTTP ์„œ๋ฒ„๋‹ค.:

$loop = React\EventLoop\Factory::create();

$server = stream_socket_server('tcp://127.0.0.1:8080');
stream_set_blocking($server, false);

$loop->addReadStream($server, function($server) use ($loop){
    $conn = stream_socket_accept($server);
    $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
    $loop->addWriteStream($conn, function ($conn) use (&$data, $loop){
        $written = fwrite($conn, $data);
        if($written === strlen($data)){
            fclose($conn);
            $loop->removeWriteStream($conn);
        }else{
            $data = substr($data, $written);
        }
    });
});

$loop->addPeriodicTimer(5, function(){
    $memory = memory_get_usage() / 1024;
    $formatted = number_format($memory, 3).'K';
    echo "Current memory usage: {$formatted}\n";
})