Example #1
0
 public function connect(ConnectionInterface $conn)
 {
     $conn->write(sprintf("Hello %s!\n", $conn->id));
     $conn->on('wait', function () use($conn) {
         $conn->write("Please wait until a partner connects.\n");
     });
     $conn->on('pipe', function ($source) use($conn) {
         $message = "You are now talking to %s.\n";
         $conn->write(sprintf($message, $source->id));
     });
     $this->app->connect($conn);
 }
Example #2
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  */
 public function runRequest(ServerRequestInterface $request, ResponseInterface $response)
 {
     $queue = clone $this->queue;
     $response = $queue->run($request, $response);
     $reasonPhrase = $response->getReasonPhrase();
     $reasonPhrase = $reasonPhrase ? ' ' . $reasonPhrase : '';
     $this->connection->write(sprintf("HTTP/%s %d%s\r\n", $response->getProtocolVersion(), $response->getStatusCode(), $reasonPhrase));
     // additional headers
     foreach ($response->getHeaders() as $header => $values) {
         foreach ($values as $value) {
             $this->connection->write(sprintf("%s: %s\r\n", $header, $value));
         }
     }
     $this->connection->write("\r\n");
     $this->connection->end((string) $response->getBody());
     $this->emit('end');
 }
Example #3
0
 private function handleConnection(ConnectionInterface $connection)
 {
     $decoder = new Decoder(function (RequestInterface $request) use($connection) {
         $cb = function (Response $response) use($connection) {
             foreach ($response->toRecords() as $r) {
                 $connection->write($r->encode());
             }
         };
         $this->requestHandler->handle($request, $cb);
     });
     $connection->pipe($decoder);
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function send($data)
 {
     return $this->conn->write($data);
 }
 /**
  * Executes a Doctrine command.
  *
  * @param \React\Socket\ConnectionInterface $conn    The connection resource
  * @param array                             $command The array with the command elements to execute
  *
  * @return void
  */
 public function doctrine($conn, $command)
 {
     // switch to the new runlevel
     $result = $this->synchronized(function ($self, $cmd) {
         // wait till the previous commands has been finished
         while ($self->locked === true) {
             sleep(1);
         }
         // set connection and command name
         $self->command = DoctrineCommand::COMMAND;
         // lock process
         $self->locked = true;
         $self->params = $cmd;
         $self->result = null;
         // notify the AS to execute the command
         $self->notify();
         // wait for the result of the command
         while ($self->result == null) {
             $self->wait(10000);
         }
         // return the result
         return $self->result;
     }, $this, $command);
     // write the result to the output
     $conn->write($result);
 }