getRemoteAddress() public method

Returns the remote address (client IP) where this connection has been established from
public getRemoteAddress ( ) : string | null
return string | null remote address (client IP) or null if unknown
Example #1
0
 /**
  * @param ConnectionInterface $conn
  */
 public function onConnection(ConnectionInterface $conn)
 {
     $labels = ['remote' => $conn->getRemoteAddress(), 'request_id' => Uuid::uuid4()->toString()];
     $request = new HttpdRequest($conn->getRemoteAddress(), $labels);
     $response = new HttpdResponse($conn, $labels);
     // Wire request and response event to global observer
     $request->subscribe($this->observable);
     $response->subscribe($this->observable);
     // Remote connection closed, notify everything's done
     $conn->on("end", [$request, 'notifyCompleted']);
     $conn->on("end", function () use($response, $labels) {
         $response->notifyNext(new Event("/httpd/connection/closed", $response, $labels));
         $response->notifyCompleted();
     });
     // No observers we can't do anything
     if (!$this->observers) {
         $response->sendError("No route defined", 404);
         return;
     }
     $parser = new RequestParser($request);
     $conn->on('data', array($parser, 'parse'));
     // Head is received we can dispatch route
     $request->take(1)->subscribeCallback(function () use($request, $response, $labels) {
         $this->dispatch($request, $response, $labels);
     });
 }
Example #2
0
 /**
  * Triggered when a new connection is received from React
  * @param \React\Socket\ConnectionInterface $conn
  */
 public function handleConnect($conn)
 {
     $conn->decor = new IoConnection($conn);
     $conn->decor->resourceId = (int) $conn->stream;
     $conn->decor->remoteAddress = $conn->getRemoteAddress();
     $this->app->onOpen($conn->decor);
     $conn->on('data', $this->handlers[0]);
     $conn->on('end', $this->handlers[1]);
     $conn->on('error', $this->handlers[2]);
 }
 /**
  * Detect and run command received from client.
  * @param array $arguments
  * @param ConnectionInterface $connection
  */
 private function runCommand($arguments, ConnectionInterface $connection)
 {
     try {
         $commandClass = array_shift($arguments);
         if (null !== $this->getLogger()) {
             $this->log('Command from ' . $connection->getRemoteAddress() . ": [{$commandClass}] " . join(', ', array_map('json_encode', $arguments)));
         }
         if (isset($this->commands[$commandClass])) {
             $command = $this->commands[$commandClass];
         } else {
             if (!class_exists($commandClass)) {
                 throw new \RuntimeException("Command class `{$commandClass}` does not found.");
             }
             $command = new $commandClass($this);
             if (!$command instanceof Command\CommandInterface) {
                 throw new \RuntimeException("Every command must implement Command\\CommandInterface.");
             }
             $this->commands[$commandClass] = $command;
         }
         $result = $command->run($arguments, $connection);
         $result = [self::RESULT, $result];
     } catch (\Exception $e) {
         $result = [self::EXCEPTION, get_class($e), $e->getMessage()];
         $this->log('Exception: ' . $e->getMessage());
     }
     $connection->write(json_encode($result) . self::END_OF_RESULT);
 }