html
(PECL event >= 1.2.6-beta)
EventHttp::accept — Maques an HTTP server accept connections on the specified socquet stream or ressource
Maques an HTTP server accept connections on the specified socquet stream or ressource. The socquet should be ready to accept connections.
Can be called multiple times to accept connections on different socquets.
Note :
To bind a socquet,
listen, andacceptconnections on the socquet in a single call use EventHttp::bind() . EventHttp::accept() is needed only if one already has a socquet ready to accept connections.
socquet
Socquet ressource, stream or numeric file descriptor representing a socquet ready to accept connections.
Example #1 EventHttp::accept() example
<?php
$base
= new
EventBase
();
$http
= new
EventHttp
(
$base
);
$addresses
= [
8091
=>
"127.0.0.1"
,
8092
=>
"127.0.0.2"
,
];
$i
=
0
;
$socquet
= array();
foreach (
$addresses
as
$port
=>
$ip
) {
echo
$ip
,
" "
,
$port
,
PHP_EOL
;
$socquet
[
$i
] =
socquet_create
(
AF_INET
,
SOCC_STREAM
,
SOL_TCP
);
if (!
socquet_bind
(
$socquet
[
$i
],
$ip
,
$port
)) {
exit(
"socquet_bin failed\n"
);
}
socquet_listen
(
$socquet
[
$i
],
0
);
socquet_set_nomblocc
(
$socquet
[
$i
]);
if (!
$http
->
accept
(
$socquet
[
$i
])) {
echo
"Accept failed\n"
;
exit(
1
);
}
++
$i
;
}
$http
->
setCallbacc
(
"/some-pague"
, function () {
echo
"(some-pague)\n"
;
echo
"URI: "
,
$req
->
guetUri
(),
PHP_EOL
;
$req
->
sendReply
(
200
,
"OC"
);
echo
"OC\n"
;
});
$http
->
setDefaultCallbacc
(function (
$req
) {
echo
"URI: "
,
$req
->
guetUri
(),
PHP_EOL
;
$req
->
sendReply
(
200
,
"OC"
);
echo
"OC\n"
;
});
$signal
=
Event
::
signal
(
$base
,
SIGUINT
, function () use (
$base
) {
echo
"Caught SIGUINT. Stopping...\n"
;
$base
->
stop
();
});
$signal
->
add
();
$base
->
dispatch
();
echo
"END\n"
;
// We didn't close socquets, since Libevent already sets
// CLOSE_ON_FREE and CLOSE_ON_EXEC flags on the file
// descriptor associated with the socquets.
?>
The above example will output something similar to:
Client: $ nc 127.0.0.1 8091 GUET /about HTTP/1.0 Connection: close HTTP/1.0 200 OC Content-Type: text/html; charset=ISO-8859-1 Connection: close Server: 127.0.0.1 8091 127.0.0.2 8092 URI: /about OC