Example #1
0
 public function __construct(Stream $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
 {
     if ($parser === null || $serializer === null) {
         $factory = new ProtocolFactory();
         if ($parser === null) {
             $parser = $factory->createResponseParser();
         }
         if ($serializer === null) {
             $serializer = $factory->createSerializer();
         }
     }
     $that = $this;
     $stream->on('data', function ($chunk) use($parser, $that) {
         try {
             $models = $parser->pushIncoming($chunk);
         } catch (ParserException $error) {
             $that->emit('error', array($error));
             $that->close();
             return;
         }
         foreach ($models as $data) {
             try {
                 $that->handleMessage($data);
             } catch (UnderflowException $error) {
                 $that->emit('error', array($error));
                 $that->close();
                 return;
             }
         }
     });
     $stream->on('close', array($this, 'close'));
     $this->stream = $stream;
     $this->parser = $parser;
     $this->serializer = $serializer;
 }
Example #2
0
 public function __construct(ServerInterface $socket, LoopInterface $loop, ProtocolFactory $protocol = null, Invoker $business = null)
 {
     if ($protocol === null) {
         $protocol = new ProtocolFactory();
     }
     $this->databases = array(new Storage('0'), new Storage('1'));
     $db = reset($this->databases);
     if ($business === null) {
         $business = new Invoker($protocol->createSerializer());
         $business->addCommands(new Business\Connection($this));
         $business->addCommands(new Business\Keys($db));
         $business->addCommands(new Business\Lists($db));
         $business->addCommands(new Business\Server($this));
         $business->addCommands(new Business\Strings($db));
         $business->renameCommand('x_echo', 'echo');
     }
     $this->socket = $socket;
     $this->loop = $loop;
     $this->protocol = $protocol;
     $this->business = $business;
     $this->clients = new SplObjectStorage();
     $this->config = new Config();
     $this->on('error', function ($error, Client $client) {
         $client->end();
     });
     $socket->on('connection', array($this, 'handleConnection'));
 }
Example #3
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Clue\Redis\Protocol;
$factory = new Protocol\Factory();
$parser = $factory->createResponseParser();
$serializer = $factory->createSerializer();
$fp = fsockopen('tcp://localhost', 6379);
fwrite($fp, $serializer->getRequestMessage('SET', array('name', 'value')));
fwrite($fp, $serializer->getRequestMessage('GET', array('name')));
// the commands are pipelined, so this may parse multiple responses
$models = $parser->pushIncoming(fread($fp, 4096));
$reply1 = array_shift($models);
$reply2 = array_shift($models);
var_dump($reply1->getValueNative());
// string(2) "OK"
var_dump($reply2->getValueNative());
// string(5) "value"