/** * Performs a websocket handshake * * @param string $data * @throws BadRequestException * @throws HandshakeException * @throws WrenchException */ public function handshake($data) { try { list($path, $origin, $key, $extensions, $protocol, $headers, $params) = $this->protocol->validateRequestHandshake($data); $this->headers = $headers; $this->queryParams = $params; $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->log(sprintf('Handshake successful: %s:%d (%s) connected to %s', $this->getIp(), $this->getPort(), $this->getId(), $path), 'info'); $this->manager->getServer()->notify(Server::EVENT_HANDSHAKE_SUCCESSFUL, array($this)); if (method_exists($this->application, 'onConnect')) { $this->application->onConnect($this); } } catch (WrenchException $e) { $this->log('Handshake failed: ' . $e, 'err'); $this->close($e); } }