OpenSwoole\Event->set

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Event::set(int $fd, mixed $read_callback, mixed $write_callback, int $flags)\:\ bool

Parameters

fd

fd of the socket type

read_callback

Readable callback function

write_callback

Writeable callback function

flags

Flags: OpenSwoole\Constant::EVENT_READ OpenSwoole\Constant::EVENT_WRITE OpenSwoole\Constant::EVENT_READ|OpenSwoole\Constant::EVENT_WRITE

Return

success

If success, it returns TRUE, meaning the event listener was successfully added

Otherwise it returns FALSE, meaning there was an issue registering the event

Description

Update the read, write callback function and flags registered on the Event Loop, which is based on the file descriptor passed in. The Event::set method is basically the same as Event:add but allows you to update an existing registered event listener. If the file descriptor passed in does not exist, false is returned because the fd does not exist.

When either the read or write functions are set to null, nothing is modified, allowing you to only change the flags.

Only call Event::del when clearing an event listener, by doing this, the event loop will release read_callback or write_callback when a event callback is cleared, again, this must be done when wanting to perform other operations on a socket. You can use Event:set to alter the status and event monitoring for the file descriptor.

Note: If you enable the OpenSwoole\Constant::EVENT_READ event, but it is not currently set up via the read_callback, false will be returned. The same is true for OpenSwoole\Constant::EVENT_WRITE.

Example

<?php

$fd = inotify_init();

// Setup a new event listener for inotify read events
OpenSwoole\Event::add($fd, function () use ($fd) {

    $var = inotify_read($fd);
    var_dump($var);

});

// Set to monitor and listen for read events for the given $fd
OpenSwoole\Event::set($fd, null, null, OpenSwoole\Constant::EVENT_READ);
Last updated on September 20, 2022