<?php
Swoole\Event->add(mixed $sock, mixed $read_callback, mixed $write_callback, int $flags)
fd
, stream
, sockets
, stream_socket_client
, fsockopen
Read callback function
Write callback function
flags
:
SWOOLE_EVENT_READ
SWOOLE_EVENT_WRITE
SWOOLE_EVENT_READ|SWOOLE_EVENT_WRITE
if success, it returns TRUE, otherwise it returns FALSE.
Regist the read, write callback function and flags of a sock
on the Event Loop.
<?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);
});