-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy path57-server-error-handling.php
More file actions
33 lines (23 loc) · 839 Bytes
/
57-server-error-handling.php
File metadata and controls
33 lines (23 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Message\ResponseFactory;
use React\Http\Server;
use React\Promise\Promise;
require __DIR__ . '/../vendor/autoload.php';
$loop = Factory::create();
$count = 0;
$server = new Server($loop, function (ServerRequestInterface $request) use (&$count) {
return new Promise(function ($resolve, $reject) use (&$count) {
$count++;
if ($count%2 === 0) {
throw new Exception('Second call');
}
$response = ResponseFactory::plain("Hello World!\n");
$resolve($response);
});
});
$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$server->listen($socket);
echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;
$loop->run();