(PECL event >= 1.2.6-beta)
EventBufferEvent::connect — 将缓冲区事件的文件描述符连接到给定的地址或 UNIX 套接字
将缓冲区事件的文件描述符连接到给定的地址(可选地带端口)或 UNIX 域套接字。
如果套接字未分配给缓冲区事件,则此函数会分配一个新的套接字并在内部使其变为非阻塞。
要解析 DNS 名称(异步),请使用 EventBufferEvent::connectHost() 方法。
addr
应包含带可选端口号的 IP 地址或 UNIX 域套接字的路径。识别的格式是
[IPv6Address]:port [IPv6Address] IPv6Address IPv4Address:port IPv4Address unix:path
'unix:'
前缀目前不区分大小写。
示例 #1 EventBufferEvent::connect() 示例
<?php
/*
* 1. 通过 EventBufferEvent::connect() 连接到端口 80 的 127.0.0.1。
*
* 2. 使用输出缓冲区通过 HTTP/1.0 请求 /index.cphp。
*
* 3. 异步读取响应并将其打印到标准输出。
*/
/* 读取回调 */
function readcb($bev, $base) {
$input = $bev->getInput();
while (($n = $input->remove($buf, 1024)) > 0) {
echo $buf;
}
}
/* 事件回调 */
function eventcb($bev, $events, $base) {
if ($events & EventBufferEvent::CONNECTED) {
echo "已连接。\n";
} elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
if ($events & EventBufferEvent::ERROR) {
echo "DNS 错误: ", $bev->getDnsErrorString(), PHP_EOL;
}
echo "关闭\n";
$base->exit();
exit("完成\n");
}
}
$base = new EventBase();
echo "步骤 1\n";
$bev = new EventBufferEvent($base, /* 使用内部套接字 */ NULL,
EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS);
if (!$bev) {
exit("创建缓冲区事件套接字失败\n");
}
echo "步骤 2\n";
$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
$bev->enable(Event::READ | Event::WRITE);
echo "步骤 3\n";
/* 发送请求 */
$output = $bev->getOutput();
if (!$output->add(
"GET /index.cphp HTTP/1.0\r\n".
"Connection: Close\r\n\r\n"
)) {
exit("将请求添加到输出缓冲区失败\n");
}
/* 异步连接到主机。
* 我们知道 IP,不需要解析 DNS。 */
if (!$bev->connect("127.0.0.1:80")) {
exit("无法连接到主机\n");
}
/* 分派挂起的事件 */
$base->dispatch();
上面的示例将输出类似于以下内容:
step 1 step 2 step 3 Connected. HTTP/1.1 200 OK Server: nginx/1.2.6 Date: Sat, 09 Mar 2013 10:06:58 GMT Content-Type: text/html; charset=utf-8 Connection: close X-Powered-By: PHP/5.4.11--pl2-gentoo sdfsdfsf Closing Done
示例 #2 连接到一个可能由服务器提供的 UNIX 域套接字,从服务器读取响应并将其输出到控制台
<?php
class MyUnixSocketClient {
private $base, $bev;
function __construct($base, $sock_path) {
$this->base = $base;
$this->bev = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE,
array ($this, "read_cb"), NULL, array ($this, "event_cb"));
if (!$this->bev->connect("unix:$sock_path")) {
trigger_error("Failed to connect to socket `$sock_path'", E_USER_ERROR);
}
$this->bev->enable(Event::READ);
}
function __destruct() {
if ($this->bev) {
$this->bev->free();
$this->bev = NULL;
}
}
function dispatch() {
$this->base->dispatch();
}
function read_cb($bev, $unused) {
$in = $bev->input;
printf("Received %ld bytes\n", $in->length);
printf("----- data ----\n");
printf("%ld:\t%s\n", (int) $in->length, $in->pullup(-1));
$this->bev->free();
$this->bev = NULL;
$this->base->exit(NULL);
}
function event_cb($bev, $events, $unused) {
if ($events & EventBufferEvent::ERROR) {
echo "Error from bufferevent\n";
}
if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {
$bev->free();
$bev = NULL;
} elseif ($events & EventBufferEvent::CONNECTED) {
$bev->output->add("test\n");
}
}
}
if ($argc <= 1) {
exit("Socket path is not provided\n");
}
$sock_path = $argv[1];
$base = new EventBase();
$cl = new MyUnixSocketClient($base, $sock_path);
$cl->dispatch();
?>
上面的示例将输出类似于以下内容:
Received 5 bytes ----- data ---- 5: test