示例#1
0
 /**
  * Start & init the API
  */
 public function listen()
 {
     // Init API
     $this->apiManager->init();
     if (!$this->apiManager->connect()) {
         throw new \RuntimeException('There is no ready client, aborted');
     }
     // Init server
     $loop = $this->apiManager->getLoop();
     $socket = new SocketServer($loop);
     $socket->on('connection', function ($conn) {
         /** @var Connection $conn */
         $this->logger->debug(sprintf('Client [%s] is connected to server', $conn->getRemoteAddress()));
         // On receive data
         $conn->on('data', function ($rawData) use($conn) {
             $this->logger->debug(sprintf('Client sent: %s', $rawData));
             $data = json_decode($rawData, true);
             $format = null;
             if (isset($data['format'])) {
                 $format = $data['format'];
             }
             try {
                 if (!$this->isValidInput($data)) {
                     throw new MalformedClientInputException('The input sent to the server is maformed');
                 }
                 $conn->on('api-response', function ($response) use($conn, $format) {
                     $conn->end($this->format($response, $format));
                 });
                 $conn->on('api-error', function (ServerException $e) use($conn, $format) {
                     $conn->end($this->format($e->toArray(), $format));
                     if ($e instanceof RequestTimeoutException) {
                         $e->getClient()->reconnect();
                         // Force doing heartbeats to check if another client is timed out
                         $this->apiManager->doHeartbeats();
                     }
                 });
                 $this->apiManager->getRouter()->process($this->apiManager, $conn, $data);
             } catch (ServerException $e) {
                 $this->logger->error('Client [' . $conn->getRemoteAddress() . ']: ' . $e->getMessage());
                 $conn->end($this->format($e->toArray(), $format));
             }
         });
     });
     $port = ConfigurationLoader::get('server.port');
     $bind = ConfigurationLoader::get('server.bind');
     $this->logger->info(sprintf('Listening on %s:%d', $bind == '0.0.0.0' ? '*' : $bind, $port));
     $socket->listen($port, $bind);
     $loop->run();
 }
示例#2
0
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->writeSection($output, 'EloGank - League of Legends API');
     $apiManager = new ApiManager();
     try {
         $server = new Server($apiManager);
         $server->listen();
     } catch (\Exception $e) {
         $this->getApplication()->renderException($e, $output);
         $apiManager->getLogger()->critical($e);
         $apiManager->clean();
         // Need to be killed manually, see ReactPHP issue: https://github.com/reactphp/react/issues/296
         posix_kill(getmypid(), SIGKILL);
     }
 }
示例#3
0
 /**
  * Call another controller method
  *
  * @param string $route
  * @param array  $parameters
  *
  * @return mixed
  */
 protected function call($route, array $parameters = array())
 {
     $this->apiManager->getRouter()->process($this->apiManager, $this->conn, array('route' => $route, 'region' => $this->region, 'parameters' => $parameters));
 }