/** * Process events on a client socket * * @param resource $socket */ protected function processClientSocket($socket) { $connection = $this->getConnectionForClientSocket($socket); if (!$connection) { $this->logger->warn('No connection for client socket'); return; } try { $connection->process(); } catch (CloseException $e) { $this->logger->error('Client connection closed', $e); $connection->close($e); } catch (WrenchException $e) { $this->logger->error('Error on client socket', $e); $connection->close($e); } }
/** * Performs a websocket handshake * * @param string $data * @throws BadRequestException * @throws HandshakeException * @throws WrenchException */ public function handshake($data) { try { list($path, $origin, $key, $extensions) = $this->protocol->validateRequestHandshake($data); $this->application = $this->manager->getApplicationForPath($path); if (!$this->application) { throw new BadRequestException('Invalid application'); } $this->manager->getServer()->notify(Server::EVENT_HANDSHAKE_REQUEST, array($this, $path, $origin, $key, $extensions)); $response = $this->protocol->getResponseHandshake($key); if (!$this->socket->isConnected()) { throw new HandshakeException('Socket is not connected'); } if ($this->socket->send($response) === false) { throw new HandshakeException('Could not send handshake response'); } $this->handshaked = true; $this->logger->info(sprintf('Handshake successful: %s:%d (%s) connected to %s', $this->getIp(), $this->getPort(), $this->getId(), $path)); $this->manager->getServer()->notify(Server::EVENT_HANDSHAKE_SUCCESSFUL, array($this)); if (method_exists($this->application, 'onConnect')) { $this->application->onConnect($this); } } catch (WrenchException $e) { $this->logger->error('Handshake failed', $e); $this->close($e); } }