想自己了解了异步编程的流程, 写的demo 里libevent 绑定读事件无效的问题,大门帮看下??
server.php
<?php
$soc = stream_socket_server('tcp://0.0.0.0:2032');
var_dump($soc);
stream_set_blocking($soc, 0);
$eventBase = event_base_new();
$event = event_new();
event_set($event, $soc, EV_READ | EV_PERSIST, 'accpet', null);
event_base_set($event, $eventBase);
event_add($event);
event_base_loop($eventBase);
function accpet() {
global $soc;
global $eventBase;
$new_soc = stream_socket_accept($soc, 0, $remote_ip);
stream_set_blocking($new_soc, 0);
// while ($str = fread($new_soc, 1024)) {
// error_log($str.'------111');
// }
if (is_resource($new_soc)) {
$event = event_new();
$ret1 = event_set($event, $new_soc, EV_READ | EV_PERSIST, 'evread', null);
$ret2 = event_base_set($event, $eventBase);
$ret3 = event_add($event);
}
}
function evread() {
error_log(1111111);
}
client.php
<?php
$errno = '';
$errmsg = '';
$soc = stream_socket_client('tcp://0.0.0.0:2032', $errno, $errmsg, 10, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
stream_set_blocking($soc, 0);
if (empty($soc)) {
echo $errno.'-----'.$errmsg.PHP_EOL;
} else {
$ret = fwrite($soc, "asdasdasdadeeeeeeeee\r\n");
var_dump($ret);
}
当client.php 向 $soc里写入数据的时候,server.php利用fread($new_soc, 1024)是可以读出数据的,但用异步的方式后, 流程并没有走到evread函数了, 哪位懂的能帮解释下,谢了
自答。发现只要加一个 全局变量events, 然后在accpet里将 event赋值给events 就行了。 然后具体原因还没找到...