-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvents.php
48 lines (44 loc) · 1.49 KB
/
Events.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* PHP的socket编程方式有过很多种
* 1.PHP 自带的select() 方法
* 2.基于pecl的event库(linux需要安装libevent开发库)
* 3.基于pecl的libevent库 (linux需要安装libevent开发库,PHP7.0 以上libevent扩展不稳定,存在问题,php7以下可以使用)
* event是在libevent基础上实现基于事件驱动的扩展库,它有着高效并发处理能力。基于它来实现一个H5的websocket的DEMO,仅供学习参考,请不要用于生产环境。
* 本类基于event的事件管理类
* */
class Events
{
protected $eventBase;
protected $allEvents = [];
public function __construct()
{
if (!extension_loaded('event')) {
echo 'event extension is require' . PHP_EOL;
exit(250);
}
$this->eventBase = new \EventBase();
}
public function add($fd, $flag, $func, $args = array())
{
$fd_key = (int)$fd;
$event = new \Event($this->eventBase, $fd, $flag | \Event::PERSIST, $func, $fd);
if (!$event || !$event->add()) {
return false;
}
$this->allEvents[$fd_key][$flag] = $event;
return true;
}
public function del($fd, $flag)
{
$fd_key = (int)$fd;
if (isset($this->allEvents[$fd_key][$flag])) {
$this->allEvents[$fd_key][$flag]->del();
unset($this->allEvents[$fd_key][$flag]);
}
}
public function loop()
{
$this->eventBase->loop();
}
}