https://github.com/WyriHaximus/reactphp-reactjs-hostname-analyzer-example
$loop = React\EventLoop\Factory::create();
$loop->run();
$socket = new SocketServer($loop);
$http = new HttpServer($socket, $loop);
$http->on(
'request',
function ($request, $response) {
// Handle Request
}
);
$socket->listen(2406);
if ($request->getPath() !== '/sse') {
return;
}
$response->writeHead(
200,
['Content-Type' =>
'text/event-stream']
);
$channel->connect($response);
$response->on('close', function () {
$channel->disconnect($response);
});
function broadcast($event)
{
$this->
channel->
writeMessage(
json_encode($event->data())
);
}
if ($request->getPath() !==
'/lookup.json') {
return;
}
lookUp('php.net');
$response->writeHead(200);
$response->end('{}');
$files = Filesystem::create($loop);
$files = Filesystem::create($loop)
->dir('/var/www/app/public');
$files = Filesystem::create($loop)
->dir('/var/www/app/public')
->lsRecursive();
foreach ($listing as $node) {
if (!($node instanceof FileInterface)) {
continue;
}
}
if ($request->getPath() ==
$node->getPath()) {
$response->writeHead(200);
$node->pipe($response);
}
use React\Dns\Resolver\Factory;
$factory = new ResolverFactory();
$resolver = $factory->
createCached('8.8.8.8', $loop);
$resolver->resolve('php.net');
resolve()->then(function ($ip) {
broadcast([
'type' => 'ip',
'data' => $ip,
]);
});
$httpClient = new Client([
'handler' => new Adapter(
$loop,
null,
$dns
),
]);
$httpClient->getAsync(
'https://freegeoip.net/json/php.net'
)
getAsync()->then(function ($response) {
$body = $response->
getBody()->getContents();
broadcast([
'type' => 'geoip',
'data' => json_decode($body),
]);
}
$httpClient->getAsync('http://php.net');
getAsync()->then(function ($response) {
$body = $response->
getBody()->
getContents();
if (hasTitle($body)) {
broadcast([
'type' => 'title',
'data' => getTitle($body),
]);
}
});
Github/Twitter: @WyriHaximus/@WyriHaximus
Rate my talk please: https://joind.in/talk/b625e
Slides: https://slides.com/wyrihaximus/getting-started-with-reactphp-dpc-2016
Code: https://github.com/WyriHaximus/reactphp-reactjs-hostname-analyzer-example
Blog: https://blog.wyrihaximus.net/categories/reactphp-series/