/** * 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]); }
/** * @param React\EventLoop\LoopInterface $loop * @param client $client * @param array $config */ public function __construct(React\EventLoop\LoopInterface $loop, client $client, array $config = null) { $this->loop = $loop; $this->client = $client; if ($config !== null) { $this->config = $config; } $this->id = $client->getId() . self::SCENARIO_ID_POSTFIX; $this->client->on('command', function ($command) { $this->emit('command', [$command, $this->client]); $this->clientCommand($command); }); $this->prepare(); }