/** * Connect to server and retrun promise with client. * * @return \React\Promise\PromiseInterface */ public function createClient() { return $this->factory->createClient($this->savePath)->then(function ($client) { $this->server->log('Connected with success to Redis server.'); return $client; }, function ($e) { $this->server->log('Application got an error on try to connect with Redis server: %s.', [$e->getMessage()]); throw $e; }); }
<?php use Clue\React\Redis\Client; use Clue\React\Redis\Factory; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $factory = new Factory($loop); $factory->createClient()->then(function (Client $client) { $client->incr('test'); $client->get('test')->then(function ($result) { var_dump($result); }); $client->end(); }); $loop->run();
<?php use Clue\React\Redis\Client; use Clue\React\Redis\Factory; use Clue\Redis\Protocol\Model\StatusReply; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $factory = new Factory($loop); $channel = isset($argv[1]) ? $argv[1] : 'channel'; $factory->createClient()->then(function (Client $client) use($channel) { $client->subscribe($channel)->then(function () { echo 'Now subscribed to channel ' . PHP_EOL; }); $client->on('message', function ($channel, $message) { echo 'Message on ' . $channel . ': ' . $message . PHP_EOL; }); }); $loop->run();
<?php use Clue\Redis\Protocol\Model\ErrorReply; use Clue\React\Redis\Client; use Clue\React\Redis\Factory; use Clue\Redis\Protocol\Model\ModelInterface; require __DIR__ . '/../vendor/autoload.php'; $loop = React\EventLoop\Factory::create(); $factory = new Factory($loop); echo '# connecting to redis...' . PHP_EOL; $factory->createClient()->then(function (Client $client) use($loop) { echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL; $client->on('data', function (ModelInterface $data) { if ($data instanceof ErrorReply) { echo '# error reply: ' . $data->getMessage() . PHP_EOL; } else { echo '# reply: ' . json_encode($data->getValueNative()) . PHP_EOL; } }); $loop->addReadStream(STDIN, function () use($client, $loop) { $line = fgets(STDIN); if ($line === false || $line === '') { echo '# CTRL-D -> Ending connection...' . PHP_EOL; $client->end(); } else { $line = rtrim($line); if ($line === '') { } else { $params = explode(' ', $line); $method = array_shift($params); call_user_func_array(array($client, $method), $params);
/** * @param $host * @return AnonymousObservable */ public function connect($host) { $factory = new Factory($this->loop); $promise = $factory->createClient($host); return Observable::defer(function () use($promise) { $subject = new AsyncSubject(); $promise->then(function (StreamingClient $client) use($subject) { $this->client = $client; $subject->onNext($this); $subject->onCompleted(); }, [$subject, "onError"]); return $subject; }); }
$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) { $channel->writeMessage($message); }); return $client->subscribe($red); })->then(null, function ($e) { echo 'ERROR: Unable to subscribe to Redis channel: ' . $e; }); $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; echo 'Connecting to Redis PubSub channel "' . $red . '" (channel is second parameter)' . PHP_EOL; $loop->run();