Skip to content

Instantly share code, notes, and snippets.

@cboden
Created May 10, 2012 14:01
Show Gist options
  • Save cboden/2653190 to your computer and use it in GitHub Desktop.
Save cboden/2653190 to your computer and use it in GitHub Desktop.
Libevent React
<?php
require __DIR__.'/vendor/autoload.php';
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
//$loop = new React\EventLoop\StreamSelectLoop();
$loop = new React\EventLoop\LibEventLoop();
$socket = new React\Socket\Server($loop);
$i = 1;
$socket->on('connect', function ($conn) use (&$i, $loop) {
echo "new connection $i\n";
$i++;
$conn->on('data', function ($data) use ($conn, &$i) {
$response = "Hello World!\n";
$length = strlen($response);
$conn->write("HTTP 1.1 200 OK\r\n");
$conn->write("Content-Type: text/plain\r\n");
$conn->write("Content-Length: $length\r\n");
$conn->write("\r\n");
$conn->write($response);
$conn->end();
});
});
$socket->listen(8000);
$loop->run();
/*
$ telnet localhost 8000
> (hit return)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment