diff --git a/README.md b/README.md index d50d52f..485cb1b 100644 --- a/README.md +++ b/README.md @@ -13,47 +13,48 @@ Re-use your application without changing any of its code just by wrapping it in --- -###A Quick server example +###A quick server example ```php _clients = new \SplObjectStorage; - } - - public function onOpen(SocketInterface $conn) { - $this->_clients->attach($conn); - } - - public function onRecv(SocketInterface $from, $msg) { - $stack = new SocketCollection; - foreach ($this->_clients as $client) { - if ($from != $client) { - $stack->enqueue($client); - } - } - - $command = new SendMessage($stack); - $command->setMessage($msg); - return $command; - } - - public function onClose(SocketInterface $conn) { - $this->_clients->detach($conn); - } + public function __construct() { + $this->_clients = new \SplObjectStorage; } + public function onOpen(SocketInterface $conn) { + $this->_clients->attach($conn); + } + + public function onRecv(SocketInterface $from, $msg) { + $stack = new Composite; + foreach ($this->_clients as $client) { + if ($from != $client) { + $message = new SendMessage($client); + $message->setMessage($msg); + + $stack->enqueue($message); + } + } + + return $stack; + } + + public function onClose(SocketInterface $conn) { + $this->_clients->detach($conn); + } +} + // Run the server application through the WebSocket protocol $server = new Server(new Socket, new WebSocket(new Chat)); $server->run('0.0.0.0', 80); diff --git a/lib/Ratchet/Command/CloseConnection.php b/lib/Ratchet/Command/CloseConnection.php index 4e36dc3..41308de 100644 --- a/lib/Ratchet/Command/CloseConnection.php +++ b/lib/Ratchet/Command/CloseConnection.php @@ -1,23 +1,21 @@ _sockets = $sockets; + public function __construct(SocketInterface $sockets) { + $this->_socket = $sockets; } function execute() { - foreach ($this->_sockets as $socket) { - $socket->close(); - } + $this->_socket->close(); } } \ No newline at end of file diff --git a/lib/Ratchet/Command/CommandInterface.php b/lib/Ratchet/Command/CommandInterface.php index 57db1f7..986a747 100644 --- a/lib/Ratchet/Command/CommandInterface.php +++ b/lib/Ratchet/Command/CommandInterface.php @@ -1,17 +1,16 @@ enqueue($cmd); + + return $cmd; + } + + public function enqueue(CommandInterface $command) { + return parent::enqueue($command); + } + + public function execute() { + $this->setIteratorMode(static::IT_MODE_DELETE); + + foreach ($this as $command) { + $command->execute(); + } + } +} \ No newline at end of file diff --git a/lib/Ratchet/Command/Null.php b/lib/Ratchet/Command/Null.php index c9c326b..010f132 100644 --- a/lib/Ratchet/Command/Null.php +++ b/lib/Ratchet/Command/Null.php @@ -1,12 +1,12 @@ _socket = $sockets; + public function __construct(SocketInterface $socket) { + $this->_socket = $socket; } /** @@ -26,8 +26,6 @@ class Runtime implements CommandInterface { } public function execute() { - foreach ($this->_sockets as $socket) { - return call_user_func($this->_command, $socket); - } + return call_user_func($this->_command, $socket); } } \ No newline at end of file diff --git a/lib/Ratchet/Command/SendMessage.php b/lib/Ratchet/Command/SendMessage.php index 69b49e6..014beaf 100644 --- a/lib/Ratchet/Command/SendMessage.php +++ b/lib/Ratchet/Command/SendMessage.php @@ -1,23 +1,23 @@ _sockets = $sockets; + public function __construct(SocketInterface $socket) { + $this->_socket = $socket; } /** @@ -45,8 +45,6 @@ class SendMessage implements CommandInterface { throw new \UnexpectedValueException("Message is empty"); } - foreach ($this->_sockets as $current) { - $current->write($this->_message, strlen($this->_message)); - } + $this->_socket->write($this->_message, strlen($this->_message)); } } \ No newline at end of file diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php index efb0a45..80e5a55 100644 --- a/lib/Ratchet/Protocol/WebSocket.php +++ b/lib/Ratchet/Protocol/WebSocket.php @@ -8,6 +8,7 @@ use Ratchet\SocketInterface; use Ratchet\SocketObserver; use Ratchet\Command\CommandInterface; use Ratchet\Command\SendMessage; +use Ratchet\Command\Composite; /** * The adapter to handle WebSocket requests/responses @@ -16,7 +17,6 @@ use Ratchet\Command\SendMessage; * @todo Make sure this works both ways (client/server) as stack needs to exist on client for framing * @todo Clean up Client/Version stuff. This should be a factory making single instances of Version classes, implement chain of reponsibility for version - client should implement an interface? * @todo Make sure all SendMessage Commands are framed, not just ones received from onRecv - * @todo Logic is flawed with Command/SocketCollection and framing - framing is done based on the protocol version of the received, not individual receivers... */ class WebSocket implements ProtocolInterface { /** @@ -77,12 +77,12 @@ class WebSocket implements ProtocolInterface { $header = $response; } - $to = new \Ratchet\SocketCollection; - $to->enqueue($from); - $cmd = new \Ratchet\Command\SendMessage($to); - $cmd->setMessage($header); + $cmds = new Composite; + $mess = new SendMessage($from); + $mess->setMessage($header); + $cmds->enqueue($mess); - return $cmd; + return $cmds; } try { @@ -91,19 +91,26 @@ class WebSocket implements ProtocolInterface { $msg = $msg['payload']; } } catch (\UnexpectedValueException $e) { - $to = new \Ratchet\SocketCollection; - $to->enqueue($from); - $cmd = new \Ratchet\Command\Close($to); + $cmd = new Composite; + $close = new \Ratchet\Command\Close($from); + $cmd->enqueue($close); return $cmd; } - $cmd = $this->_app->onRecv($from, $msg); - if ($cmd instanceof SendMessage) { - $cmd->setMessage($client->getVersion()->frame($cmd->getMessage())); + $cmds = $this->_app->onRecv($from, $msg); + if ($cmds instanceof Composite) { + foreach ($cmds as $cmd) { + if ($cmd instanceof SendMessage) { + $sock = $cmd->_socket; + $clnt = $this->_clients[$sock]; + + $cmd->setMessage($clnt->getVersion()->frame($cmd->getMessage())); + } + } } - return $cmd; + return $cmds; } /** diff --git a/lib/Ratchet/Server.php b/lib/Ratchet/Server.php index 79717fb..38d6da1 100644 --- a/lib/Ratchet/Server.php +++ b/lib/Ratchet/Server.php @@ -4,7 +4,7 @@ use Ratchet\Server\Aggregator; use Ratchet\Protocol\ProtocolInterface; use Ratchet\Logging\LoggerInterface; use Ratchet\Logging\NullLogger; -use Ratchet\Command\CommandInterface; +use Ratchet\Command\Composite; /** * Creates an open-ended socket to listen on a port for incomming connections. Events are delegated through this to attached applications @@ -108,7 +108,7 @@ class Server implements SocketObserver, \IteratorAggregate { } } - if ($res instanceof CommandInterface) { + if ($res instanceof Composite) { $res->execute(); } } diff --git a/lib/Ratchet/SocketCollection.php b/lib/Ratchet/SocketCollection.php deleted file mode 100644 index 1c3b918..0000000 --- a/lib/Ratchet/SocketCollection.php +++ /dev/null @@ -1,18 +0,0 @@ -setIteratorMode(static::IT_MODE_DELETE); - } - - /** - * @param SocketInterface - */ - public function enqueue(SocketInterface $value) { - parent::enqueue($value); - } -} \ No newline at end of file