相关知识点
如果看不明白,可以翻开我关于网络编程的其他代码;从简单到复杂的实现
- 进程名称设置
- poll、epoll 事件模型
- 基于event事件框架的事件处理
服务端代码
<?php
// 内存数据表
class Memory
{
/**
* 事件框架容器
* @var EventBase
*/
public static $eventBase = null;
/**
* event 集合
* @var array
*/
public static $events = [
'socket' => null,
'signal' => [],
'conned' => [],
];
/**
* socket句柄
* @var resource
*/
public static $socket = null;
/**
* accept 连接集合
* @var array
*/
public static $connections = [];
}
// 设置进程名
cli_set_process_title("php-nio-event");
// 排除的模式
// 写入的无法使用;可用 `strace` 追踪底层运行
$conf = new EventConfig();
$conf->avoidMethod("select");
$conf->avoidMethod("poll");
//$conf->avoidMethod("epoll");
// 实例化 事件框架容器
Memory::$eventBase = new EventBase($conf);
$addr = "tcp://0.0.0.0:6666";
$mode = STREAM_SERVER_BIND|STREAM_SERVER_LISTEN;
Memory::$socket = stream_socket_server($addr, $errno, $errmsg, $mode);
if (! Memory::$socket) {
die("{
$errmsg} (