reqParser = new HttpRequestParser; $this->versioner = new VersionManager; $this->validator = new ToggleableValidator; $this->versioner ->enableVersion(new Version\RFC6455($this->validator)) ->enableVersion(new Version\HyBi10($this->validator)) ->enableVersion(new Version\Hixie76) ; $this->_decorating = $component; $this->connections = new \SplObjectStorage; } /** * {@inheritdoc} */ public function onOpen(ConnectionInterface $conn) { $conn->WebSocket = new \StdClass; $conn->WebSocket->established = false; } /** * {@inheritdoc} */ public function onMessage(ConnectionInterface $from, $msg) { if (true === $from->WebSocket->established) { return $from->WebSocket->version->onMessage($this->connections[$from], $msg); } if (isset($from->WebSocket->request)) { $from->WebSocket->request->getBody()->write($msg); } else { try { if (null === ($request = $this->reqParser->onMessage($from, $msg))) { return; } } catch (\OverflowException $oe) { return $this->close($from, 413); } if (!$this->versioner->isVersionEnabled($request)) { return $this->close($from); } $from->WebSocket->request = $request; $from->WebSocket->version = $this->versioner->getVersion($request); } try { $response = $from->WebSocket->version->handshake($from->WebSocket->request); } catch (\UnderflowException $e) { return; } // This needs to be refactored later on, incorporated with routing if ('' !== ($agreedSubProtocols = $this->getSubProtocolString($from->WebSocket->request->getTokenizedHeader('Sec-WebSocket-Protocol', ',')))) { $response->setHeader('Sec-WebSocket-Protocol', $agreedSubProtocols); } $response->setHeader('X-Powered-By', \Ratchet\VERSION); $from->send((string)$response); if (101 != $response->getStatusCode()) { return $from->close(); } $upgraded = $from->WebSocket->version->upgradeConnection($from, $this->_decorating); $this->connections->attach($from, $upgraded); $upgraded->WebSocket->established = true; return $this->_decorating->onOpen($upgraded); } /** * {@inheritdoc} */ public function onClose(ConnectionInterface $conn) { if ($this->connections->contains($conn)) { $decor = $this->connections[$conn]; $this->connections->detach($conn); $this->_decorating->onClose($decor); } } /** * {@inheritdoc} */ public function onError(ConnectionInterface $conn, \Exception $e) { if ($conn->WebSocket->established) { $this->_decorating->onError($this->connections[$conn], $e); } else { $conn->close(); } } /** * Disable a specific version of the WebSocket protocol * @param int $versionId Version ID to disable * @return WsServer */ public function disableVersion($versionId) { $this->versioner->disableVersion($versionId); return $this; } /** * Toggle weather to check encoding of incoming messages * @param bool * @return WsServer */ public function setEncodingChecks($opt) { $this->validator->on = (boolean)$opt; return $this; } /** * @param string * @return boolean */ public function isSubProtocolSupported($name) { if (!$this->isSpGenerated) { if ($this->_decorating instanceof WsServerInterface) { $this->acceptedSubProtocols = array_flip($this->_decorating->getSubProtocols()); } $this->isSpGenerated = true; } return array_key_exists($name, $this->acceptedSubProtocols); } /** * @param \Traversable|null $requested * @return string */ protected function getSubProtocolString(\Traversable $requested = null) { if (null === $requested) { return ''; } $result = array(); foreach ($requested as $sub) { if ($this->isSubProtocolSupported($sub)) { $result[] = $sub; } } return implode(',', $result); } /** * Close a connection with an HTTP response * @param \Ratchet\ConnectionInterface $conn * @param int $code HTTP status code * @return void */ protected function close(ConnectionInterface $conn, $code = 400) { $response = new Response($code, array( 'Sec-WebSocket-Version' => $this->versioner->getSupportedVersionString() , 'X-Powered-By' => \Ratchet\VERSION )); $conn->send((string)$response); $conn->close(); } }