示例#1
0
 /**
  * Start server
  *
  * @throws React\Socket\ConnectionException
  */
 public function start()
 {
     $this->emit('starting', [$this]);
     $socketServer = $this->socketServer();
     $socketServer->on('connection', function (React\Socket\Connection $clientConnection) {
         $clientConnection->pause();
         $newClient = new client($clientConnection->stream, $this->loop, new $this->config['protocol']());
         $clientId = $newClient->getId();
         $this->clients[$clientId] = $newClient;
         /** @var communicationInterface $communication */
         $communicationClass = $this->config['communication'];
         $innerConfig = isset($this->config['inner']) ? $this->config['inner'] : null;
         $communication = new $communicationClass($this->loop, $newClient, $innerConfig);
         $this->communications[$communication->getId()] = $communication;
         $newClient->on('close', function () use($newClient, $communication) {
             $communication->emit('disconnected');
             unset($this->communications[$communication->getId()], $this->clients[$newClient->getId()]);
         });
         $newClient->resume();
         $this->emit('connection', [$newClient, $communication]);
         $communication->emit('connected');
     });
     $socketServer->listen($this->config['port'], $this->config['host']);
     $this->emit('started', [$this]);
 }