forked from moonprism/websocket.php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibevent.php
More file actions
56 lines (49 loc) · 1.16 KB
/
Copy pathLibevent.php
File metadata and controls
56 lines (49 loc) · 1.16 KB
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
49
50
51
52
53
54
55
56
<?php
class Libevent
{
private static $instance;
public $base = null;
public $events = [];
private function __construct(){}
/**
* 单例
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new self();
self::$instance->base = event_base_new();
}
return self::$instance;
}
/**
* 添加事件轮询
* @param socket $fd 需要监听的socket操作
* @param string|[class, string] $fun 回调函数名
*/
public function add($fd, $fun)
{
// 创建一个新的事件
$event = event_new();
event_set($event, $fd, EV_READ | EV_PERSIST, $fun);
event_base_set($event, $this->base);
event_add($event);
$this->events[intval($fd)] = $event;
}
/**
* 启动事件轮询
*/
public function loop()
{
event_base_loop($this->base);
}
/**
* 移除事件轮询
* @param int $flag 事件标志intval($fd)
*/
public function del($flag)
{
event_del($this->events[$flag]);
unset($this->events[$flag]);
}
}