Swoole EventLoop

Developers can use swoole Event Loop API to use the system Event Loop.

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

Alias: public static boolean swoole_event_add ( int $fd , string $read_callback [, string $write_callback [, string $events ]] )

Add new callback functions of a socket into the EventLoop.

Example:

<?php
$fp = stream_socket_client("tcp://www.qq.com:80", $errno, $errstr, 30);
fwrite($fp,"GET / HTTP/1.1\r\nHost: www.qq.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";

public static boolean Swoole\Event::set ( int $fd [, string $read_callback [, string $write_callback [, string $events ]]] )

Alias: public static boolean swoole_event_set ( int $fd [, string $read_callback [, string $write_callback [, string $events ]]] )

Update the event callback functions of a socket.

public static boolean Swoole\Event::del ( string $fd )

Alias: public static boolean swoole_event_del ( string $fd )

Remove all event callback functions of a socket.

public static void Swoole\Event::exit ( void )

Alias: public static void swoole_event_exit ( void )

Exit the eventloop, only available at client side.

public static void Swoole\Event::wait ( void )

Alias: public static void swoole_event_wait ( void )

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

Alias: public static void swoole_event_write ( string $fd , string $data )

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);

public static void Swoole\Event::defer ( mixed $callback )

Alias: public static void swoole_event_defer ( mixed $callback )

Add callback function to the next event loop.

Example:

<?php
swoole_event_defer(function(){
    echo "After EventLoop\n";
});