EventHttp::accept

(PECL event >= 1.2.6-beta)

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

Description

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.

Parameters

socket

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

Return Values

Returns true on success or false on failure.

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" "$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.
?>

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

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial