diff --git a/lib/Ratchet/Command/Action/CloseConnection.php b/lib/Ratchet/Command/Action/CloseConnection.php index 46fed4e..391e857 100644 --- a/lib/Ratchet/Command/Action/CloseConnection.php +++ b/lib/Ratchet/Command/Action/CloseConnection.php @@ -2,6 +2,9 @@ namespace Ratchet\Command\Action; use Ratchet\Command\ActionTemplate; use Ratchet\SocketObserver; +use Ratchet\SocketInterface; +use Ratchet\Command\CommandInterface; +use Ratchet\Command\Composite; /** * Close the connection to the sockets passed in the constructor @@ -9,8 +12,21 @@ use Ratchet\SocketObserver; class CloseConnection extends ActionTemplate { function execute(SocketObserver $scope = null) { $ret = $scope->onClose($this->getSocket()); - $this->getSocket()->close(); - return $ret; + if ($ret instanceof CommandInterface) { + $comp = new Composite; + $comp->enqueue($ret); + + $rt = new Runtime($this->getSocket()); + $rt->setCommand(function(SocketInterface $socket, SocketObserver $scope) { + $socket->close(); + }); + + $comp->enqueue($rt); + + return $comp; + } + + $this->getSocket()->close(); } } \ No newline at end of file diff --git a/lib/Ratchet/Command/Action/Runtime.php b/lib/Ratchet/Command/Action/Runtime.php index 4809b4d..f357b75 100644 --- a/lib/Ratchet/Command/Action/Runtime.php +++ b/lib/Ratchet/Command/Action/Runtime.php @@ -10,7 +10,7 @@ class Runtime extends ActionTemplate { protected $_command = null; /** - * Your closure should accept a single \Ratchet\SocketInterface parameter and return a CommandInterface or NULL + * Your closure should accept two parameters (\Ratchet\SocketInterface, \Ratchet\SocketObserver) parameter and return a CommandInterface or NULL * @param Closure Your closure/lambda to execute when the time comes */ public function setCommand(\Closure $callback) { @@ -18,6 +18,6 @@ class Runtime extends ActionTemplate { } public function execute(SocketObserver $scope = null) { - return call_user_func($this->_command, $this->getSocket()); + return call_user_func($this->_command, $this->getSocket(), $scope); } } \ No newline at end of file diff --git a/lib/Ratchet/Command/Factory.php b/lib/Ratchet/Command/Factory.php index 79ba037..68217b8 100644 --- a/lib/Ratchet/Command/Factory.php +++ b/lib/Ratchet/Command/Factory.php @@ -2,6 +2,9 @@ namespace Ratchet\Command; use Ratchet\SocketInterface; +/** + * A factory pattern class to easily create all the things in the Ratchet\Command interface + */ class Factory { protected $_paths = array(); diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php index 8171a09..22edec3 100644 --- a/lib/Ratchet/Protocol/WebSocket.php +++ b/lib/Ratchet/Protocol/WebSocket.php @@ -14,27 +14,27 @@ use Ratchet\Protocol\WebSocket\Util\HTTP; * This is a mediator between the Server and your application to handle real-time messaging through a web browser * @link http://ca.php.net/manual/en/ref.http.php * @todo Make sure this works both ways (client/server) as stack needs to exist on client for framing - * @todo Make sure all SendMessage Commands are framed, not just ones received from onRecv * @todo Learn about closing the socket. A message has to be sent prior to closing - does the message get sent onClose event or CloseConnection command? + * @todo Consider cheating the application...don't call _app::onOpen until handshake is complete - only issue is sending headers/cookies */ class WebSocket implements ProtocolInterface { - /** - * @var Ratchet\Command\Factory - */ - protected $_factory; - /** * Lookup for connected clients - * @type SplObjectStorage + * @var SplObjectStorage */ protected $_clients; /** * Decorated application - * @type Ratchet\SocketObserver + * @var Ratchet\SocketObserver */ protected $_app; + /** + * @var Ratchet\Command\Factory + */ + protected $_factory; + /** * @internal */ @@ -107,6 +107,12 @@ class WebSocket implements ProtocolInterface { public function onClose(SocketInterface $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; } @@ -139,7 +145,7 @@ class WebSocket implements ProtocolInterface { /** * @param array of HTTP headers - * @return Version\VersionInterface + * @return WebSocket\Version\VersionInterface */ protected function getVersion($message) { $headers = HTTP::getHeaders($message); @@ -156,7 +162,7 @@ class WebSocket implements ProtocolInterface { } /** - * @return Version\VersionInterface + * @return WebSocket\Version\VersionInterface */ protected function versionFactory($version) { if (null === $this->_versions[$version]) { diff --git a/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php b/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php index 3f9ac1c..ecb662c 100644 --- a/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php +++ b/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php @@ -1,13 +1,9 @@