Swoole\Event->add

Declaration

<?php
Swoole\Event->add(mixed $sock, mixed $read_callback, mixed $write_callback, int $flags)

Parameters

sock

fd, stream, sockets, stream_socket_client, fsockopen

read_callback

Read callback function

write_callback

Write callback function

flags

flags: SWOOLE_EVENT_READ SWOOLE_EVENT_WRITE SWOOLE_EVENT_READ|SWOOLE_EVENT_WRITE

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Regist the read, write callback function and flags of a sock on the Event Loop.

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