Developers can use swoole EventLoop API to use the system EventLoop.
bool swoole_event_add(int $sock, mixed $read_callback, mixed $write_callback = null, int $flags = null);
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";
Update the event callback functions of a socket.
Remove all event callback functions of a socket.
Exit the eventloop, only available at client side.
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);
Add callback function to the next event loop.
Example:
<?php
swoole_event_defer(function(){
echo "After EventLoop\n";
});