libevent php扩展安装
libevent php扩展安装
hanhang126 发表于2年前
libevent php扩展安装
  • 发表于 2年前
  • 阅读 36
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: libevent安装 libevent php扩展安装
  1. 下载libevent-2.0.22-stable.tar.gz 下载官网地址:http://libevent.org/

  2. 下载php扩展libevent-0.1.0.tgz 下载官网地址:http://pecl.php.net/package/libevent

  3. 安装libevent-2.0.22

tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable/
./configure --prefix=/usr/local/libevent-2.0.20-stable/
sudo make
sudo make install
  1. 安装扩展libevent-0.1.0
tar zxvf libevent-0-1.0.0.tar  
cd libevent-0.1.0  
/usr/bin/phpize    
./configure  --with-php-config=/usr/bin/php-config --with-libevent=/usr/local/libevent-2.0.22-stable
sudo make  
sudo make install  
  1. 配置php.ini
cd /etc/php5/mods-available/
sudo touch libevent.ini
sudo vi libevent.ini  (输入extension=libevent.so保存)
cd /etc/php5/cli/conf.d
sudo ln -s ../../mods-available/libevent.ini 01-libevent.ini
cd /etc/php5/fpm/conf.d
sudo ln -s ../../mods-available/libevent.ini 01-libevent.ini
  1. 重启相关服务
sudo service php5-fpm restart
sudo service nginx restart
  1. 测试程序
sudo touch http-server.php
<?php
$socket = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr);
stream_set_blocking($socket, 0);
$base = event_base_new();
$event = event_new();
event_set($event, $socket, EV_READ | EV_PERSIST, 'ev_accept', $base);
event_base_set($event, $base);
event_add($event);
event_base_loop($base);

$GLOBALS['connections'] = array();
$GLOBALS['buffers'] = array();

function ev_accept($socket, $flag, $base) {
    static $id = 0;
    
    $connection = stream_socket_accept($socket);
    stream_set_blocking($connection, 0);
    
    $id += 1;
    
    $buffer = event_buffer_new($connection, 'ev_read', NULL, 'ev_error', $id);
    event_buffer_base_set($buffer, $base);
    event_buffer_timeout_set($buffer, 30, 30);
    event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
    event_buffer_priority_set($buffer, 10);
    event_buffer_enable($buffer, EV_READ | EV_PERSIST);
    
    // we need to save both buffer and connection outside
    $GLOBALS['connections'][$id] = $connection;
    $GLOBALS['buffers'][$id] = $buffer;
}

function ev_error($buffer, $error, $id) {
    event_buffer_disable($GLOBALS['buffers'][$id], EV_READ | EV_WRITE);
    event_buffer_free($GLOBALS['buffers'][$id]);
    fclose($GLOBALS['connections'][$id]);
    unset($GLOBALS['buffers'][$id], $GLOBALS['connections'][$id]);
}

function ev_read($buffer, $id) {
    while ($read = event_buffer_read($buffer, 256)) {
        var_dump($read);
    }
}
?>

  1. 启动测试
php http-server.php

另打开一个终端,输入:

telnet <ip> <port>
标签: libevent php
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 4
码字总数 1047
×
hanhang126
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: