Skip to content

Commit 2fecc1e

Browse files
committed
Fix sending buffered messages when reconnecting
1 parent f5feeee commit 2fecc1e

7 files changed

Lines changed: 27 additions & 24 deletions

File tree

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
language: php
22

33
php:
4+
- 5.3
45
- 5.4
56
- 5.5
67
- 5.6
78
- 7.0
89
- 7.1
910
- 7.2
1011
- 7.3
11-
- 7.4
1212

1313
# lock distro so new future defaults will not break the build
14-
dist: trusty
15-
16-
sudo: false
14+
dist: precise
1715

1816
install:
1917
- composer install --no-interaction

examples/01-simple-periodic.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
$channel = new BufferedChannel();
1313

14-
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel) {
14+
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
1515
if ($request->getUri()->getPath() === '/') {
1616
return new Response(
1717
200,
@@ -26,9 +26,12 @@
2626

2727
echo 'connected' . PHP_EOL;
2828

29-
$id = $request->getHeaderLine('Last-Event-ID');
3029
$stream = new ThroughStream();
31-
$channel->connect($stream, $id);
30+
31+
$id = $request->getHeaderLine('Last-Event-ID');
32+
$loop->futureTick(function () use ($channel, $stream, $id) {
33+
$channel->connect($stream, $id);
34+
});
3235

3336
$stream->on('close', function () use ($stream, $channel) {
3437
echo 'disconnected' . PHP_EOL;

examples/02-plaintext-chat.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,18 @@
2727

2828
echo 'connected' . PHP_EOL;
2929

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

32+
$id = $request->getHeaderLine('Last-Event-ID');
33+
$loop->futureTick(function () use ($channel, $stream, $id) {
34+
$channel->connect($stream, $id);
35+
});
36+
3437
$stream->on('close', function () use ($stream, $channel) {
3538
echo 'disconnected' . PHP_EOL;
3639
$channel->disconnect($stream);
3740
});
3841

39-
$loop->futureTick(function () use ($channel, $stream, $id) {
40-
$channel->connect($stream, $id);
41-
});
42-
4342
return new Response(
4443
200,
4544
array('Content-Type' => 'text/event-stream'),

examples/03-redis-subscribe.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
$channel = new BufferedChannel();
1414

15-
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel) {
15+
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
1616
if ($request->getUri()->getPath() === '/') {
1717
return new Response(
1818
'200',
@@ -27,10 +27,12 @@
2727

2828
echo 'connected' . PHP_EOL;
2929

30-
$id = $request->getHeaderLine('Last-Event-ID');
31-
3230
$stream = new ThroughStream();
33-
$channel->connect($stream, $id);
31+
32+
$id = $request->getHeaderLine('Last-Event-ID');
33+
$loop->futureTick(function () use ($channel, $stream, $id) {
34+
$channel->connect($stream, $id);
35+
});
3436

3537
$stream->on('close', function () use ($stream, $channel) {
3638
echo 'disconnected' . PHP_EOL;

examples/11-chat.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
$channel = new BufferedChannel();
1313

14-
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel) {
15-
$stream = new ThroughStream();
16-
14+
$http = new React\Http\Server($loop, function (ServerRequestInterface $request) use ($channel, $loop) {
1715
switch ($request->getUri()->getPath()) {
1816
case '/':
1917
return new Response(
@@ -39,9 +37,12 @@
3937
array('Content-Type' => 'text/json')
4038
);
4139
case '/chat':
42-
$id = $request->getHeaderLine('Last-Event-ID');
40+
$stream = new ThroughStream();
4341

44-
$channel->connect($stream, $id);
42+
$id = $request->getHeaderLine('Last-Event-ID');
43+
$loop->futureTick(function () use ($channel, $stream, $id) {
44+
$channel->connect($stream, $id);
45+
});
4546

4647
$serverParams = $request->getServerParams();
4748
$message = array('message' => 'New person connected from '. $serverParams['REMOTE_ADDR']);

src/BufferedChannel.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Clue\React\Sse;
44

5-
use React\Stream\Stream;
65
use React\Stream\WritableStreamInterface;
76

87
/**

src/Encoder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
class Encoder
66
{
77
const EOL = "\n";
8+
89
/**
910
* write a single message field name and value pair (should not include newlines)
1011
*
1112
* should not be be used to write data field (multiline contents)
1213
*
1314
* @param string $name
14-
* @param value $value
15+
* @param string $value
1516
* @return string
1617
*/
1718
public function encodeField($name, $value)

0 commit comments

Comments
 (0)