Swoole EventLoop

Developers can use swoole Event Loop API to use the 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";
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";
});