Swoole EventLoop API for Linux

Swoole Event Loop API to use the Linux system Event Loop.

Swoole\Event::add ( int $fd , string $read_callback [, string $write_callback [, string $events ]] ) : bool

Add new callback functions of a socket into the EventLoop.

Example:

<?php
$fp = stream_socket_client("tcp://www.google.com:80", $errno, $errstr, 30);
fwrite($fp,"GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n");

Swoole\Event::add($fp, function($fp) {
    $resp = fread($fp, 8192);
    // Remove the socket from eventloop
    Swoole\Event::del($fp);
    fclose($fp);
});
echo "Finish\n";

Manage fd with the EventLoop, for example monitoring file changes with inotify:

<?php
$fd = inotify_init();
Swoole\Event::add($fd, function () use ($fd){
    $var = inotify_read($fd);
    var_dump($var);
});
Swoole\Event::set ( int $fd [, string $read_callback [, string $write_callback [, string $events ]]] ) : bool

Update the event callback functions of a socket.

Swoole\Event::del ( string $fd ) : bool

Remove all event callback functions of a socket.

Swoole\Event::exit ( void ) : void

Exit the eventloop, only available at client side.

Swoole\Event::wait ( void ) : void
Swoole\Event::write ( string $fd , string $data ) : void

Write data to the socket.

Example:

<?php
$fp = stream_socket_client('tcp://127.0.0.1:9501');
$data = str_repeat('A', 1024 * 1024*2);

Swoole\Event::add($fp, function($fp) {
     echo fread($fp);
});

Swoole\Event::write($fp, $data);
Swoole\Event::defer ( mixed $callback ) : void

Add callback function to the next event loop.

Example:

<?php
Swoole\Event::defer(function(){
    echo "After EventLoop\n";
});