PHP code example of 7csn / reactor

1. Go to this page and download the library: Download 7csn/reactor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

7csn / reactor example snippets


    
  
    use chaser\reactor\Driver;  
    use chaser\reactor\Reactor;  
  
    // composer 自加载,路径视具体情况而定
    

        # 增/改
        $driver->setWrite(resource $fd, callable $callback): bool;
          # 内部回调
          $callback($fd);
      
        # 删
        $driver->delWrite(resource $fd): bool;
        

        # 增/改
        $driver->setSignal(int $signal, callable $callback): bool;
          # 内部回调
          $callback($signal);
      
        # 删
        $driver->delSignal(int $signal): bool;
        

        # 增(失败返回 0)
        $driver->setInterval(int $seconds, callable $callback): $timerId;
          # 内部回调
          $callback($timerId);
      
        # 删
        $driver->delInterval(int $timerId): bool;
        

        # 增(失败返回 0)
        $driver->setTimeout(int $seconds, callable $callback): $timerId;
          # 内部回调
          $callback($timerId);
      
        # 删
        $driver->delTimeout(int $timerId): bool;
        

        $driver->set(resource $fd|int $signal|int $seconds, int $flag, callable $callback): bool|$timerId
        $driver->del(resource $fd|int $signal|int $timerId, int $flag): bool
          # 事件类型
          $flag:
              Reactor::EV_READ        # 套接字读
              Reactor::EV_WRITE       # 套接字写
              Reactor::EV_SIGNAL      # 信号
              Reactor::EV_INTERVAL    # 周期性定时器
              Reactor::EV_TIMEOUT     # 一次性定时器
        

    $driver->loop(): void;
    

    $driver->destroy(): void;
    

    // 默认统计全部数量
    $driver->getCount(int $flag = null): void;
    

    // 默认清空所有事件侦听
    $driver->clear(int $flag = null): void;
    

    // 默认清空所有定时器事件侦听
    $driver->clearTimer(): void;