Fixed bad interface implementation in Close Command
Removed old code from Composite (now in Factory)
Removed done @todo's
Cleaned up Ping/Pong
Added HyBi-10 frame/unframe test
This commit is contained in:
Chris Boden 2011-11-07 11:55:07 -05:00
parent 1ba8021df8
commit 60a8a04e40
8 changed files with 19 additions and 35 deletions

View File

@ -12,8 +12,8 @@ class CloseConnection implements CommandInterface {
*/ */
protected $_socket; protected $_socket;
public function __construct(SocketInterface $sockets) { public function __construct(SocketInterface $socket) {
$this->_socket = $sockets; $this->_socket = $socket;
} }
function execute() { function execute() {

View File

@ -3,28 +3,6 @@ namespace Ratchet\Command;
use Ratchet\SocketInterface; use Ratchet\SocketInterface;
class Composite extends \SplQueue { class Composite extends \SplQueue {
/**
* @param string
* @param Ratchet\SocketInterface
* @return CommandInterface
*/
public function NOPEcreate($name, SocketInterface $socket) {
$class = __NAMESPACE__ . "\\{$name}\\";
if (!class_exists($class)) {
throw new \UnexpectedValueException("Command {$name} not found");
}
$cmd = new $class($socket);
if ($cmd instanceof CommandInterface) {
throw new RuntimeException("{$name} is not a valid command");
}
$this->enqueue($cmd);
return $cmd;
}
public function enqueue(CommandInterface $command) { public function enqueue(CommandInterface $command) {
return parent::enqueue($command); return parent::enqueue($command);
} }

View File

@ -15,7 +15,6 @@ 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 * 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 * @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 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 Make sure all SendMessage Commands are framed, not just ones received from onRecv
*/ */
class WebSocket implements ProtocolInterface { class WebSocket implements ProtocolInterface {
@ -36,7 +35,7 @@ class WebSocket implements ProtocolInterface {
public function __construct(SocketObserver $application) { public function __construct(SocketObserver $application) {
$this->_clients = new \SplObjectStorage; $this->_clients = new \SplObjectStorage;
$this->_app = $application; $this->_app = $application;
} }
/** /**
@ -58,6 +57,9 @@ class WebSocket implements ProtocolInterface {
return $this->_app->onOpen($conn); return $this->_app->onOpen($conn);
} }
/**
* @todo Cleanup spaghetti code
*/
public function onRecv(SocketInterface $from, $msg) { public function onRecv(SocketInterface $from, $msg) {
$client = $this->_clients[$from]; $client = $this->_clients[$from];
if (true !== $client->isHandshakeComplete()) { if (true !== $client->isHandshakeComplete()) {
@ -102,7 +104,7 @@ class WebSocket implements ProtocolInterface {
if ($cmds instanceof Composite) { if ($cmds instanceof Composite) {
foreach ($cmds as $cmd) { foreach ($cmds as $cmd) {
if ($cmd instanceof SendMessage) { if ($cmd instanceof SendMessage) {
$sock = $cmd->_socket; $sock = $cmd->_socket; // bad
$clnt = $this->_clients[$sock]; $clnt = $this->_clients[$sock];
$cmd->setMessage($clnt->getVersion()->frame($cmd->getMessage())); $cmd->setMessage($clnt->getVersion()->frame($cmd->getMessage()));

View File

@ -1,10 +1,8 @@
<?php <?php
namespace Ratchet\Command; namespace Ratchet\Command;
use Ratchet\SocketInterface; use Ratchet\SocketInterface;
use Ratchet\Command\CommandInterface;
/**
* @todo Move this command to the WebSocket protocol namespace
*/
class Ping implements CommandInterface { class Ping implements CommandInterface {
public function __construct(SocketInterface $socket) { public function __construct(SocketInterface $socket) {
} }

View File

@ -1,10 +1,8 @@
<?php <?php
namespace Ratchet\Command; namespace Ratchet\Command;
use Ratchet\SocketInterface; use Ratchet\SocketInterface;
use Ratchet\Command\CommandInterface;
/**
* @todo Move this command to the WebSocket protocol namespace
*/
class Pong implements CommandInterface { class Pong implements CommandInterface {
public function __construct(SocketInterface $socket) { public function __construct(SocketInterface $socket) {
} }

View File

@ -30,8 +30,9 @@ class HyBi10 implements VersionInterface {
* Thanks to @lemmingzshadow for the code on decoding a HyBi-10 frame * Thanks to @lemmingzshadow for the code on decoding a HyBi-10 frame
* @link https://github.com/lemmingzshadow/php-websocket * @link https://github.com/lemmingzshadow/php-websocket
* @param string * @param string
* @return string * @return array
* @throws UnexpectedValueException * @throws UnexpectedValueException
* @todo return a common interface instead of array
*/ */
public function unframe($message) { public function unframe($message) {
$data = $message; $data = $message;

View File

@ -9,7 +9,6 @@ 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 * 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 Consider using _connections as master reference and passing iterator_to_array(_connections) to socket_select
* @todo Move SocketObserver methods to separate class, create, wrap class in __construct
* @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 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
*/ */
class Server implements SocketObserver, \IteratorAggregate { class Server implements SocketObserver, \IteratorAggregate {

View File

@ -47,4 +47,12 @@ class HyBi10Test extends \PHPUnit_Framework_TestCase {
, array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=') , array("The quick brown fox jumps over the lazy dog. All work and no play makes Chris a dull boy. I'm trying to get past 128 characters for a unit test here...", 'gf4Amahb14P8M7Kj2S6+4MN7tfHHLLmjzjSvo8IuuvPbe7j1zSn398A+9+/JIa6jzDSwrYh7lu/Ee6Ds2jD34sY/9+3He6fvySL37skwsvCIGL/xwSj34og/ou/Ee7Xs0XX3o+F8uqPcKa7qxjz398d7sObce6fi2y/3sppj9+DAOqXiyy+y8dt7sezae7aj3TW+94gvsvDce7/m2j75rYY=')
); );
} }
public function testUnframeMatchesPreFraming() {
$string = 'Hello World!';
$framed = $this->_version->frame($string);
$unframed = $this->_version->unframe($framed);
$this->assertEquals($string, $unframed['payload']);
}
} }