EventHttp::accept
(PECL event >= 1.2.6-beta)
EventHttp::accept — Makes an HTTP server accept connections on the specified socket stream or resource
Description
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.
Parameters
-
socket
-
Socket resource, stream or numeric file descriptor representing a socket ready to accept connections.
Examples
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, " ", $port, PHP_EOL;
$socket[$i] = socket_create(AF_INET, SOCK_STREAM, SOL_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($base, SIGINT, 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.
?>
The above example will output something similar to:
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
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-eventhttp.accept.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.