Source

pecl-event / examples / http_accept.php

<?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->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.
?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.