Skip to content

Commit 251f6c6

Browse files
authored
Merge pull request #11 from clue-labs/update
Update to latest ReactPHP components
2 parents 67bfa07 + 2fecc1e commit 251f6c6

9 files changed

Lines changed: 145 additions & 112 deletions

File tree

.travis.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ php:
77
- 5.6
88
- 7.0
99
- 7.1
10+
- 7.2
11+
- 7.3
1012

11-
# also test against HHVM, but require "trusty" and ignore errors
12-
matrix:
13-
include:
14-
- php: hhvm
15-
dist: trusty
16-
allow_failures:
17-
- php: hhvm
13+
# lock distro so new future defaults will not break the build
14+
dist: precise
1815

1916
install:
2017
- composer install --no-interaction

composer.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
},
1616
"require": {
1717
"php": ">=5.3",
18-
"react/event-loop": "~0.4.0|~0.3.0",
19-
"react/http": "^0.4.4",
20-
"react/stream": "~0.4.0|~0.3.0"
18+
"react/event-loop": "^1.0",
19+
"react/http": "^1.0",
20+
"react/stream": "^1.0"
2121
},
2222
"require-dev": {
23-
"clue/redis-react": "^0.5",
24-
"phpunit/phpunit": "^5.0 || ^4.8",
25-
"react/socket-client": "^0.5"
23+
"clue/redis-react": "^2.4",
24+
"phpunit/phpunit": "^5.0 || ^4.8"
2625
}
2726
}

examples/01-simple-periodic.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,56 @@
33
require __DIR__ . '/../vendor/autoload.php';
44

55
use Clue\React\Sse\BufferedChannel;
6-
use React\Http\Request;
7-
use React\Http\Response;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
use React\Http\Message\Response;
8+
use React\Stream\ThroughStream;
89

910
$loop = React\EventLoop\Factory::create();
10-
$socket = new React\Socket\Server($loop);
1111

1212
$channel = new BufferedChannel();
1313

14-
$http = new React\Http\Server($socket);
15-
$http->on('request', function (Request $request, Response $response) use ($channel) {
16-
if ($request->getPath() === '/') {
17-
$response->writeHead('200', array('Content-Type' => 'text/html'));
18-
$response->end(file_get_contents(__DIR__ . '/00-eventsource.html'));
19-
return;
14+
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
15+
if ($request->getUri()->getPath() === '/') {
16+
return new Response(
17+
200,
18+
array('Content-Type' => 'text/html'),
19+
file_get_contents(__DIR__ . '/00-eventsource.html')
20+
);
2021
}
2122

22-
if ($request->getPath() !== '/demo') {
23-
$response->writeHead(404);
24-
$response->end('Not Found');
25-
return;
23+
if ($request->getUri()->getPath() !== '/demo') {
24+
return new Response(404);
2625
}
2726

2827
echo 'connected' . PHP_EOL;
2928

30-
$id = $request->getHeaderLine('Last-Event-ID');
29+
$stream = new ThroughStream();
3130

32-
$response->writeHead(200, array('Content-Type' => 'text/event-stream'));
33-
$channel->connect($response, $id);
31+
$id = $request->getHeaderLine('Last-Event-ID');
32+
$loop->futureTick(function () use ($channel, $stream, $id) {
33+
$channel->connect($stream, $id);
34+
});
3435

35-
$response->on('close', function () use ($response, $channel) {
36+
$stream->on('close', function () use ($stream, $channel) {
3637
echo 'disconnected' . PHP_EOL;
37-
$channel->disconnect($response);
38+
$channel->disconnect($stream);
3839
});
40+
41+
return new Response(
42+
200,
43+
array('Content-Type' => 'text/event-stream'),
44+
$stream
45+
);
3946
});
4047

41-
$loop->addPeriodicTimer(2.0, function() use ($channel) {
48+
$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
49+
$http->listen($socket);
50+
51+
$loop->addPeriodicTimer(2.0, function () use ($channel) {
4252
$channel->writeMessage('ticking ' . mt_rand(1, 5) . '...');
4353
});
4454

45-
$socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');
46-
47-
echo 'Server now listening on http://localhost:' . $socket->getPort() . ' (port is first parameter)' . PHP_EOL;
55+
echo 'Server now listening on ' . $socket->getAddress() . ' (port is first parameter)' . PHP_EOL;
4856
echo 'This will send a message every 2 seconds' . PHP_EOL;
4957

5058
$loop->run();

examples/02-plaintext-chat.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,52 @@
33
require __DIR__ . '/../vendor/autoload.php';
44

55
use Clue\React\Sse\BufferedChannel;
6-
use React\Http\Request;
7-
use React\Http\Response;
8-
use React\SocketClient\TcpConnector;
9-
use React\Stream\Stream;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
use React\Http\Message\Response;
8+
use React\Socket\TcpConnector;
9+
use React\Stream\ThroughStream;
1010

1111
$loop = React\EventLoop\Factory::create();
12-
$socket = new React\Socket\Server($loop);
1312

1413
$channel = new BufferedChannel();
1514

16-
$http = new React\Http\Server($socket);
17-
$http->on('request', function (Request $request, Response $response) use ($channel) {
18-
if ($request->getPath() === '/') {
19-
$response->writeHead('200', array('Content-Type' => 'text/html'));
20-
$response->end(file_get_contents(__DIR__ . '/00-eventsource.html'));
21-
return;
15+
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
16+
if ($request->getUri()->getPath() === '/') {
17+
return new Response(
18+
200,
19+
array('Content-Type' => 'text/html'),
20+
file_get_contents(__DIR__ . '/00-eventsource.html')
21+
);
2222
}
2323

24-
if ($request->getPath() !== '/demo') {
25-
$response->writeHead(404);
26-
$response->end('Not Found');
27-
return;
24+
if ($request->getUri()->getPath() !== '/demo') {
25+
return new Response(404);
2826
}
2927

3028
echo 'connected' . PHP_EOL;
3129

32-
$id = $request->getHeaderLine('Last-Event-ID');
30+
$stream = new ThroughStream();
3331

34-
$response->writeHead(200, array('Content-Type' => 'text/event-stream'));
35-
$channel->connect($response, $id);
32+
$id = $request->getHeaderLine('Last-Event-ID');
33+
$loop->futureTick(function () use ($channel, $stream, $id) {
34+
$channel->connect($stream, $id);
35+
});
3636

37-
$response->on('close', function () use ($response, $channel) {
37+
$stream->on('close', function () use ($stream, $channel) {
3838
echo 'disconnected' . PHP_EOL;
39-
$channel->disconnect($response);
39+
$channel->disconnect($stream);
4040
});
41+
42+
return new Response(
43+
200,
44+
array('Content-Type' => 'text/event-stream'),
45+
$stream
46+
);
4147
});
4248

4349
$port = isset($argv[2]) ? $argv[2] : 8000;
4450
$connector = new TcpConnector($loop);
45-
$connector->create('127.0.0.1', $port)->then(function (Stream $stream) use ($channel) {
51+
$connector->connect('127.0.0.1:' . $port)->then(function (React\Socket\ConnectionInterface $stream) use ($channel) {
4652
$buffer = '';
4753

4854
$stream->on('data', function ($data) use (&$buffer, $channel) {
@@ -55,9 +61,10 @@
5561
});
5662
}, 'printf');
5763

58-
$socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');
64+
$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
65+
$http->listen($socket);
5966

60-
echo 'Server now listening on http://localhost:' . $socket->getPort() . ' (port is first parameter)' . PHP_EOL;
67+
echo 'Server now listening on ' . $socket->getAddress() . ' (port is first parameter)' . PHP_EOL;
6168
echo 'Connecting to plain text chat on port ' . $port . ' (port is second parameter)' . PHP_EOL;
6269

6370
$loop->run();

examples/03-redis-subscribe.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,53 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
use Clue\React\Sse\BufferedChannel;
6-
use React\Http\Request;
7-
use React\Http\Response;
85
use Clue\React\Redis\Factory;
6+
use Clue\React\Sse\BufferedChannel;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use React\Http\Message\Response;
9+
use React\Stream\ThroughStream;
910

1011
$loop = React\EventLoop\Factory::create();
11-
$socket = new React\Socket\Server($loop);
1212

1313
$channel = new BufferedChannel();
1414

15-
$http = new React\Http\Server($socket);
16-
$http->on('request', function (Request $request, Response $response) use ($channel) {
17-
if ($request->getPath() === '/') {
18-
$response->writeHead('200', array('Content-Type' => 'text/html'));
19-
$response->end(file_get_contents(__DIR__ . '/00-eventsource.html'));
20-
return;
15+
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
16+
if ($request->getUri()->getPath() === '/') {
17+
return new Response(
18+
'200',
19+
array('Content-Type' => 'text/html'),
20+
file_get_contents(__DIR__ . '/00-eventsource.html')
21+
);
2122
}
2223

23-
if ($request->getPath() !== '/demo') {
24-
$response->writeHead(404);
25-
$response->end('Not Found');
26-
return;
24+
if ($request->getUri()->getPath() !== '/demo') {
25+
return new Response(404);
2726
}
2827

2928
echo 'connected' . PHP_EOL;
3029

31-
$id = $request->getHeaderLine('Last-Event-ID');
30+
$stream = new ThroughStream();
3231

33-
$response->writeHead(200, array('Content-Type' => 'text/event-stream'));
34-
$channel->connect($response, $id);
32+
$id = $request->getHeaderLine('Last-Event-ID');
33+
$loop->futureTick(function () use ($channel, $stream, $id) {
34+
$channel->connect($stream, $id);
35+
});
3536

36-
$response->on('close', function () use ($response, $channel) {
37+
$stream->on('close', function () use ($stream, $channel) {
3738
echo 'disconnected' . PHP_EOL;
38-
$channel->disconnect($response);
39+
$channel->disconnect($stream);
3940
});
41+
42+
return new Response(
43+
200,
44+
array('Content-Type' => 'text/event-stream'),
45+
$stream
46+
);
4047
});
4148

4249
$red = isset($argv[2]) ? $argv[2] : 'channel';
4350
$factory = new Factory($loop);
44-
$factory->createClient()->then(function (Clue\React\Redis\Client $client) use ($channel, $red) {
51+
$factory->createClient("localhost")->then(function (Clue\React\Redis\Client $client) use ($channel, $red) {
4552
$client->on('message', function ($topic, $message) use ($channel) {
4653
$channel->writeMessage($message);
4754
});
@@ -50,9 +57,10 @@
5057
echo 'ERROR: Unable to subscribe to Redis channel: ' . $e;
5158
});
5259

53-
$socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');
60+
$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
61+
$http->listen($socket);
5462

55-
echo 'Server now listening on http://localhost:' . $socket->getPort() . ' (port is first parameter)' . PHP_EOL;
63+
echo 'Server now listening on ' . $socket->getAddress() . ' (port is first parameter)' . PHP_EOL;
5664
echo 'Connecting to Redis PubSub channel "' . $red . '" (channel is second parameter)' . PHP_EOL;
5765

5866
$loop->run();

examples/11-chat.php

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,71 @@
33
require __DIR__ . '/../vendor/autoload.php';
44

55
use Clue\React\Sse\BufferedChannel;
6-
use React\Http\Request;
7-
use React\Http\Response;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
use React\Http\Message\Response;
8+
use React\Stream\ThroughStream;
89

910
$loop = React\EventLoop\Factory::create();
10-
$socket = new React\Socket\Server($loop);
1111

1212
$channel = new BufferedChannel();
1313

14-
$http = new React\Http\Server($socket);
15-
$http->on('request', function (Request $request, Response $response) use ($channel) {
16-
switch ($request->getPath()) {
14+
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
15+
switch ($request->getUri()->getPath()) {
1716
case '/':
18-
$response->writeHead('200', array('Content-Type' => 'text/html'));
19-
$response->end(file_get_contents(__DIR__ . '/10-eventsource.html'));
20-
21-
return;
17+
return new Response(
18+
'200',
19+
array('Content-Type' => 'text/html'),
20+
file_get_contents(__DIR__ . '/10-eventsource.html')
21+
);
2222
case '/styles.css':
23-
$response->writeHead('200', array('Content-Type' => 'text/css'));
24-
$response->end(file_get_contents(__DIR__ . '/10-styles.css'));
25-
return;
23+
return new Response(
24+
'200',
25+
array('Content-Type' => 'text/css'),
26+
file_get_contents(__DIR__ . '/10-styles.css')
27+
);
2628
case '/message':
27-
$query = $request->getQuery();
29+
$query = $request->getQueryParams();
2830
if (isset($query['username'], $query['message'])) {
29-
$message = array('message' => $query['message'], 'username' =>$query['username']);
31+
$message = array('message' => $query['message'], 'username' => $query['username']);
3032
$channel->writeMessage(json_encode($message));
3133
}
32-
$response->writeHead('201', array('Content-Type' => 'text/json'));
33-
$response->end();
34-
return;
34+
35+
return new Response(
36+
'201',
37+
array('Content-Type' => 'text/json')
38+
);
3539
case '/chat':
36-
$id = $request->getHeaderLine('Last-Event-ID');
40+
$stream = new ThroughStream();
3741

38-
$response->writeHead(200, array('Content-Type' => 'text/event-stream'));
39-
$channel->connect($response, $id);
42+
$id = $request->getHeaderLine('Last-Event-ID');
43+
$loop->futureTick(function () use ($channel, $stream, $id) {
44+
$channel->connect($stream, $id);
45+
});
4046

41-
$message = array('message' => 'New person connected from '. $request->remoteAddress);
47+
$serverParams = $request->getServerParams();
48+
$message = array('message' => 'New person connected from '. $serverParams['REMOTE_ADDR']);
4249
$channel->writeMessage(json_encode($message));
4350

44-
$response->on('close', function () use ($response, $channel, $request) {
45-
$channel->disconnect($response);
51+
$stream->on('close', function () use ($stream, $channel, $request, $serverParams) {
52+
$channel->disconnect($stream);
4653

47-
$message = array('message' => 'Bye '. $request->remoteAddress);
54+
$message = array('message' => 'Bye '. $serverParams['REMOTE_ADDR']);
4855
$channel->writeMessage(json_encode($message));
4956
});
50-
break;
57+
58+
return new Response(
59+
200,
60+
array('Content-Type' => 'text/event-stream'),
61+
$stream
62+
);
5163
default:
52-
$response->writeHead(404);
53-
$response->end('Not Found');
64+
return new Response(404);
5465
}
5566
});
5667

57-
$socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');
68+
$socket = new \React\Socket\Server(isset($argv[1]) ? '0.0.0.0:' . $argv[1] : '0.0.0.0:0', $loop);
69+
$http->listen($socket);
5870

59-
echo 'Server now listening on http://localhost:' . $socket->getPort() . ' (port is first parameter)' . PHP_EOL;
71+
echo 'Server now listening on ' . $socket->getAddress() . ' (port is first parameter)' . PHP_EOL;
6072

6173
$loop->run();

0 commit comments

Comments
 (0)