diff --git a/lib/Ratchet/Application/Server/App.php b/lib/Ratchet/Application/Server/App.php index 48c29b0..538594d 100644 --- a/lib/Ratchet/Application/Server/App.php +++ b/lib/Ratchet/Application/Server/App.php @@ -7,8 +7,6 @@ use Ratchet\Resource\Command\CommandInterface; /** * Creates an open-ended socket to listen on a port for incomming connections. Events are delegated through this to attached applications - * @todo Consider using _connections as master reference and passing iterator_to_array(_connections) to socket_select - * @todo Currently passing Socket object down the decorated chain - should be sending reference to it instead; Receivers do not interact with the Socket directly, they do so through the Command pattern * @todo With all these options for the server I should probably use a DIC */ class App implements ApplicationInterface { diff --git a/lib/Ratchet/Application/WebSocket/App.php b/lib/Ratchet/Application/WebSocket/App.php index d5fe099..93dd144 100644 --- a/lib/Ratchet/Application/WebSocket/App.php +++ b/lib/Ratchet/Application/WebSocket/App.php @@ -1,7 +1,5 @@ _clients = new \SplObjectStorage; $this->_app = $app; $this->_factory = new Factory; } @@ -71,13 +62,17 @@ class App implements ApplicationInterface, ConfiguratorInterface { } public function onOpen(Connection $conn) { - $this->_clients[$conn] = new Client; + $conn->handshake_complete = false; } public function onRecv(Connection $from, $msg) { - $client = $this->_clients[$from]; - if (true !== $client->isHandshakeComplete()) { - $response = $client->setVersion($this->getVersion($msg))->doHandshake($msg); + if (true !== $from->handshake_complete) { + + // need buffer checks in here + + $from->websocket_version = $this->getVersion($msg); + $response = $from->websocket_version->handshake($msg); + $from->handshake_complete = true; if (is_array($response)) { $header = ''; @@ -100,25 +95,18 @@ class App implements ApplicationInterface, ConfiguratorInterface { return $comp; } - $msg = $client->getVersion()->unframe($msg); + // buffer! + + $msg = $from->websocket_version->unframe($msg); if (is_array($msg)) { // temporary $msg = $msg['payload']; } - $cmds = $this->_app->onRecv($from, $msg); - return $this->prepareCommand($cmds); + return $this->prepareCommand($this->_app->onRecv($from, $msg)); } public function onClose(Connection $conn) { - $cmds = $this->prepareCommand($this->_app->onClose($conn)); - - // $cmds = new Composite if null - // $cmds->enqueue($this->_factory->newCommand('SendMessage', $conn)->setMessage( - // WebSocket close handshake, depending on version! - //)); - - unset($this->_clients[$conn]); - return $cmds; + return $this->prepareCommand($this->_app->onClose($conn)); } public function onError(Connection $conn, \Exception $e) { @@ -138,7 +126,7 @@ class App implements ApplicationInterface, ConfiguratorInterface { */ protected function prepareCommand(CommandInterface $command = null) { if ($command instanceof SendMessage) { - $version = $this->_clients[$command->getConnection()]->getVersion(); + $version = $command->getConnection()->websocket_version; return $command->setMessage($version->frame($command->getMessage())); } diff --git a/lib/Ratchet/Application/WebSocket/Client.php b/lib/Ratchet/Application/WebSocket/Client.php deleted file mode 100644 index 2bd1e98..0000000 --- a/lib/Ratchet/Application/WebSocket/Client.php +++ /dev/null @@ -1,52 +0,0 @@ -_version = $version; - return $this; - } - - /** - * @return Version\VersionInterface - */ - public function getVersion() { - return $this->_version; - } - - /** - * @param string - * @return array|string - */ - public function doHandshake($headers) { - $this->_hands_shook = true; - - return $this->_version->handshake($headers); - } - - /** - * @return bool - */ - public function isHandshakeComplete() { - return $this->_hands_shook; - } -} \ No newline at end of file diff --git a/lib/Ratchet/Resource/Connection.php b/lib/Ratchet/Resource/Connection.php index 2cbc7a4..7ffa464 100644 --- a/lib/Ratchet/Resource/Connection.php +++ b/lib/Ratchet/Resource/Connection.php @@ -29,6 +29,7 @@ class Connection { * Anyway, if you're here, it's not recommended you use this/directly interact with the socket in your App... * The command pattern (which is fully flexible, see Runtime) is the safest, desired way to interact with the socket(s). * @return Ratchet\SocketInterface + * @todo Figure out a better way to match Socket/Connection in Application and Commands */ public function getSocket() { return $this->_socket;