EventHttp::accept

(PECL event >= 1.2.6-beta)

EventHttp::acceptMakes an HTTP server accept connections on the specified socket stream or resource

说明

public EventHttp::accept ( mixed $socket ) : bool

Makes an HTTP server accept connections on the specified socket stream or resource. The socket should be ready to accept connections.

Can be called multiple times to accept connections on different sockets.

Note:

To bind a socket, listen , and accept connections on the socket in s single call use EventHttp::bind() . EventHttp::accept() is needed only if one already has a socket ready to accept connections.

参数

socket

Socket resource, stream or numeric file descriptor representing a socket ready to accept connections.

返回值

Returns TRUE on success. Otherwise FALSE.

范例

Example #1 EventHttp::accept() example

<?php
$base 
= new EventBase();
$http = new EventHttp($base);

$addresses = array (
     
8091 => "127.0.0.1",
     
8092 => "127.0.0.2",
);
$i 0;

$socket = array();

foreach (
$addresses as $port => $ip) {
    echo 
$ip" "$portPHP_EOL;
    
$socket[$i] = socket_create(AF_INETSOCK_STREAMSOL_TCP);
    if (!
socket_bind($socket[$i], $ip$port)) {
        exit(
"socket_bind failed\n");
    }
    
socket_listen($socket[$i], 0);
    
socket_set_nonblock($socket[$i]);

    if (!
$http->accept($socket[$i])) {
        echo 
"Accept failed\n";
        exit(
1);
    }

    ++
$i;
}

$http->setCallback("/some-page", function() {
 echo 
"(some-page)\n";
    echo 
"URI: "$req->getUri(), PHP_EOL;
    
$req->sendReply(200"OK");
    echo 
"OK\n";
});

$http->setDefaultCallback(function($req) {
    echo 
"URI: "$req->getUri(), PHP_EOL;
    
$req->sendReply(200"OK");
    echo 
"OK\n";
});

$signal Event::signal($baseSIGINT, function () use ($base) {
    echo 
"Caught SIGINT. Stopping...\n";
    
$base->stop();
});
$signal->add();

$base->dispatch();
echo 
"END\n";
// We didn't close sockets, since Libevent already sets
// CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file 
// descriptor associated with the sockets.
?>

以上例程的输出类似于:

Client:
$ nc 127.0.0.1 8091
GET /about HTTP/1.0
Connection: close

HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close

Server:
127.0.0.1 8091
127.0.0.2 8092
URI: /about
OK

参见

相关文章
php eventhttp makes an http server accept connections on the specified socket stream or resourcephp eventhttp adds a server alias to the http server objectphp eventhttp binds an http server on the specified address and portphp eventhttp constructs eventhttp object the http server php eventhttp removes server aliasphp eventhttp sets the what http methods are supported in requests accepted by this server and passed to user callbacksphp eventhttpconnection makes an http request over the specified connectionphp eventhttpconnection sets the ip address from which http connections are madephp socket 函数 opens a socket on port to accept connectionsphp socket 函数 export a socket extension resource into a stream that encapsulates a socketphp socket 函数 import a streamphp stream 函数 runs the equivalent of the select system call on the given arrays of streams with a timeout specified by tv sec and tv usecphp stream 函数 接受由 stream socket server 创建的套接字连接php stream 函数 receives data from a socket connected or notphp stream 函数 sends a message to a socket whether it is connected or notphp stream 函数 create an internet or unix domain server socketphp imagick returns the specified resource limitphp swoole http server bind callback function to http server by event name.php swoole http server start the swoole http server.php zookeeper return the timeout for this session only valid if the connections is currently connected ie. last watcher state is zoo connected state . this value may change after a server re connect
关注编程学问公众号