/** * Set's up the listeners for handling incoming packages * * @param ServerSocket $server */ private function onIncoming(ServerSocket $server, LoopInterface $loop) { $server->on('connection', function ($connection) use($loop) { $client = new Client($connection, $loop, $this->database); if (!$client->getIsAuthenticated()) { $client->authenticate(); } // Handle incoming data from existing connections $connection->on('data', function ($data) use($client) { if ($client->getIsAuthenticated()) { if ($client->isAddedToClients()) { $this->processUserInput($data, $client); } else { $this->handleNewClient($client); } } }); // Handling the end of connections $connection->on('end', function () use($client) { $this->clients->detach($client); }); }); }