protected function handleSse(Request $request, Response $response) { $headers = $request->getHeaders(); $id = isset($headers['Last-Event-ID']) ? $headers['Last-Event-ID'] : null; $response->writeHead(200, array('Content-Type' => 'text/event-stream')); $this->channel->connect($response, $id); $response->on('close', function () use($response) { $this->channel->disconnect($response); }); }
public function testResultingStreamBuffer() { $stream = $this->getMock('React\\Stream\\WritableStreamInterface'); $buffered = ''; $stream->expects($this->any())->method('write')->will($this->returnCallback(function ($data) use(&$buffered) { $buffered .= $data; })); $channel = new BufferedChannel(); // initially nothing $channel->writeMessage('hello', 'world'); $this->assertEquals('', $buffered); // connecting will send messages buffered in channel $channel->connect($stream, 0); $this->assertEquals("id: 0\nevent: world\ndata: hello\n\n", $buffered); }
<?php require __DIR__ . '/vendor/autoload.php'; use Clue\React\Sse\BufferedChannel; use React\Http\Request; use React\Http\Response; use React\SocketClient\TcpConnector; use React\Stream\Stream; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $channel = new BufferedChannel(); $http = new React\Http\Server($socket); $http->on('request', function (Request $request, Response $response) use($channel) { if ($request->getPath() === '/') { $response->writeHead('200', array('Content-Type' => 'text/html')); $response->end(file_get_contents(__DIR__ . '/../01-simple-periodic/eventsource.html')); return; } echo 'connected' . PHP_EOL; $headers = $request->getHeaders(); $id = isset($headers['Last-Event-ID']) ? $headers['Last-Event-ID'] : null; $response->writeHead(200, array('Content-Type' => 'text/event-stream')); $channel->connect($response, $id); $response->on('close', function () use($response, $channel) { echo 'disconnected' . PHP_EOL; $channel->disconnect($response); }); }); $port = isset($argv[2]) ? $argv[2] : 8000; $connector = new TcpConnector($loop); $connector->create('127.0.0.1', $port)->then(function (Stream $stream) use($channel) {
public function provideListeners(ListenerAcceptorInterface $acceptor) { $acceptor->addListener('sse', function ($event, $data) { $this->channel->writeMessage(json_encode($data)); }); }
<?php require __DIR__ . '/../../vendor/autoload.php'; use Clue\React\Sse\BufferedChannel; use React\Http\Request; use React\Http\Response; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $channel = new BufferedChannel(); $http = new React\Http\Server($socket); $http->on('request', function (Request $request, Response $response) use($channel) { if ($request->getPath() === '/') { $response->writeHead('200', array('Content-Type' => 'text/html')); $response->end(file_get_contents(__DIR__ . '/eventsource.html')); return; } echo 'connected' . PHP_EOL; $headers = $request->getHeaders(); $id = isset($headers['Last-Event-ID']) ? $headers['Last-Event-ID'] : null; $response->writeHead(200, array('Content-Type' => 'text/event-stream')); $channel->connect($response, $id); $response->on('close', function () use($response, $channel) { echo 'disconnected' . PHP_EOL; $channel->disconnect($response); }); }); $loop->addPeriodicTimer(2.0, function () use($channel) { $channel->writeMessage('ticking ' . mt_rand(1, 5) . '...'); }); $socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0'); echo 'Server now listening on http://localhost:' . $socket->getPort() . ' (port is first parameter)' . PHP_EOL;
<?php require __DIR__ . '/vendor/autoload.php'; use Clue\React\Sse\BufferedChannel; use React\Http\Request; use React\Http\Response; use Clue\React\Redis\Factory; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $channel = new BufferedChannel(); $http = new React\Http\Server($socket); $http->on('request', function (Request $request, Response $response) use($channel) { if ($request->getPath() === '/') { $response->writeHead('200', array('Content-Type' => 'text/html')); $response->end(file_get_contents(__DIR__ . '/../01-simple-periodic/eventsource.html')); return; } echo 'connected' . PHP_EOL; $headers = $request->getHeaders(); $id = isset($headers['Last-Event-ID']) ? $headers['Last-Event-ID'] : null; $response->writeHead(200, array('Content-Type' => 'text/event-stream')); $channel->connect($response, $id); $response->on('close', function () use($response, $channel) { echo 'disconnected' . PHP_EOL; $channel->disconnect($response); }); }); $red = isset($argv[2]) ? $argv[2] : 'channel'; $factory = new Factory($loop); $factory->createClient()->then(function (Clue\React\Redis\Client $client) use($channel, $red) { $client->on('message', function ($topic, $message) use($channel) {