diff --git a/lib/Ratchet/Application/WAMP/App.php b/lib/Ratchet/Application/WAMP/App.php index 2829b47..f97e17b 100644 --- a/lib/Ratchet/Application/WAMP/App.php +++ b/lib/Ratchet/Application/WAMP/App.php @@ -19,13 +19,10 @@ use Ratchet\Resource\Connection; * | EVENT | 8 | Server-to-Client | * +--------------+----+------------------+ * @link http://www.tavendo.de/autobahn/protocol.html - * @todo I can't make up my mind what interface to present to the server application */ class App implements WebSocketAppInterface { protected $_app; - protected static $_incoming = array(1, 2, 5, 6, 7); - public function getSubProtocol() { return 'wamp'; } @@ -42,16 +39,12 @@ class App implements WebSocketAppInterface { $conn->prefixes[$uri] = $curie; } - public function sendEvent($uri, $event) { - } - - public function onCall(Connection $conn, $id, $uri) { - } - public function onOpen(Connection $conn) { $conn->WAMP = new \StdClass; $conn->WAMP->prefixes = array(); $conn->WAMP->subscriptions = array(); + + return $this->_app->onOpen($conn); } /** @@ -64,15 +57,35 @@ class App implements WebSocketAppInterface { throw new JSONException; } - if (!in_array($json[0], static::$_incoming)) { - throw new Exception('Invalid message type'); + switch ($json[0]) { + case 1: + return $this->addPrefix($conn, $json[2], $json[1]); + break; + + case 2: + $ret = $this->_app->onCall($from, $json[1], $json[2]); + break; + + case 5: + $ret = $this->_app->onSubscribe($from, $json[1]); + break; + + case 6: + $ret = $this->_app->onUnSubscribe($from, $json[1]); + break; + + case 7: + $ret = $this->_app->onPublish($from, $json[1], $json[2]); + break; + + default: + throw new Exception('Invalid message type'); } - if ($json[0] == 1) { - $this->addPrefix($conn, $json[2], $json[1]); - } + // create method to loop through $ret + // json_encode messages, return $ret back to WebSocket - // Determine WAMP message type, call $_this->_app->on(); + return $ret; } public function __construct(ServerInterface $app) { @@ -80,9 +93,10 @@ class App implements WebSocketAppInterface { } public function onClose(Connection $conn) { - // remove all prefixes associated with connection? or will those just be destroyed w/ Connection + return $this->_app->onClose($conn); } public function onError(Connection $conn, \Exception $e) { + return $this->_app->onError($conn, $e); } } \ No newline at end of file diff --git a/lib/Ratchet/Application/WAMP/ServerInterface.php b/lib/Ratchet/Application/WAMP/ServerInterface.php index 69f484a..86b3df6 100644 --- a/lib/Ratchet/Application/WAMP/ServerInterface.php +++ b/lib/Ratchet/Application/WAMP/ServerInterface.php @@ -2,12 +2,63 @@ namespace Ratchet\Application\WAMP; use Ratchet\Resource\Connection; +/** + * A (not literal) extension of Ratchet\Application\ApplicationInterface + * onMessage is replaced by various types of messages for this protocol (pub/sub or rpc) + * @todo Thought: URI as class. Class has short and long version stored (if as prefix) + */ interface ServerInterface { + /** + * When a new connection is opened it will be passed to this method + * @param Ratchet\Resource\Connection + */ + function onOpen(Connection $conn); + + /** + * The user closed their connection + * @param Ratchet\Resource\Connection + * @return Ratchet\Resource\Command\CommandInterface|null + */ + function onClose(Connection $conn); + + /** + * @param Ratchet\Resource\Connection + * @param \Exception + * @return Ratchet\Resource\Command\CommandInterface|null + */ + function onError(Connection $conn, \Exception $e); + + /** + * An RPC call has been received + * @param Ratchet\Resource\Connection + * @param string + * @param ... + * @return Ratchet\Resource\Command\CommandInterface|null + */ function onCall(Connection $conn, $callID, $uri); + /** + * A request to subscribe to a URI has been made + * @param Ratchet\Resource\Connection + * @param ... + * @return Ratchet\Resource\Command\CommandInterface|null + */ function onSubscribe(Connection $conn, $uri); + /** + * A request to unsubscribe from a URI has been made + * @param Ratchet\Resource\Connection + * @param ... + * @return Ratchet\Resource\Command\CommandInterface|null + */ function onUnSubscribe(Connection $conn, $uri); + /** + * A client is attempting to publish content to a subscribed connections on a URI + * @param Ratchet\Resource\Connection + * @param ... + * @param string + * @return Ratchet\Resource\Command\CommandInterface|null + */ function onPublish(Connection $conn, $uri, $event); } \ No newline at end of file diff --git a/lib/Ratchet/Application/WebSocket/App.php b/lib/Ratchet/Application/WebSocket/App.php index 0900067..8753c93 100644 --- a/lib/Ratchet/Application/WebSocket/App.php +++ b/lib/Ratchet/Application/WebSocket/App.php @@ -42,6 +42,12 @@ class App implements ApplicationInterface, ConfiguratorInterface { protected $_mask_payload = false; + /** + * @deprecated + * @temporary + */ + public $accepted_subprotocols = array(); + public function __construct(ApplicationInterface $app) { $this->_app = $app; $this->_factory = new Factory; @@ -73,7 +79,6 @@ class App implements ApplicationInterface, ConfiguratorInterface { * Do handshake, frame/unframe messages coming/going in stack * @todo This needs some major refactoring * @todo "Once the client's opening handshake has been sent, the client MUST wait for a response from the server before sending any further data." - * @todo Change Header to be a class, not array|string - will make things SO much easier...right now can't do WAMP on Hixie */ public function onMessage(Connection $from, $msg) { if (true !== $from->WebSocket->handshake) { @@ -92,13 +97,16 @@ class App implements ApplicationInterface, ConfiguratorInterface { $from->WebSocket->handshake = true; if (is_array($response)) { - if ($this->_app instanceof WebSocketAppInterface) { - // Note: this logic is wrong - we're supposed to check what the client sent - // as its sub protocol and if we support any of the requested we send that back. - // This is just sending what ever one wwe support - // This will be changed when I rewrite how headers are handled - // Also...what happens if something like a logger is put between this and the sub-protocol app? - $response['Sec-WebSocket-Protocol'] = $this->_app->getSubProtocol(); + // This block is to be moved/changed later + $agreed_protocols = array(); + $requested_protocols = $from->WebSocket->headers->getTokenizedHeader('Sec-WebSocket-Protocol', ','); + foreach ($this->accepted_subprotocols as $sub_protocol) { + if (false !== $requested_protocols->hasValue($sub_protocol)) { + $agreed_protocols[] = $sub_protocol; + } + } + if (count($agreed_protocols) > 0) { + $response['Sec-WebSocket-Protocol'] = implode(',', $agreed_protocols); } $header = ''; @@ -163,14 +171,6 @@ class App implements ApplicationInterface, ConfiguratorInterface { return $this->_app->onError($conn, $e); } - /** - * Incomplete, WebSocket protocol allows client to ask to use a sub-protocol, I'm thinking/wanting to somehow implement this in an application decorated class - * @param string - * @todo Implement or delete... - */ - public function setSubProtocol($name) { - } - /** * Checks if a return Command from your application is a message, if so encode it/them * @param Ratchet\Resource\Command\CommandInterface|NULL diff --git a/lib/Ratchet/Application/WebSocket/Version/RFC6455.php b/lib/Ratchet/Application/WebSocket/Version/RFC6455.php index ad41e66..bb579cb 100644 --- a/lib/Ratchet/Application/WebSocket/Version/RFC6455.php +++ b/lib/Ratchet/Application/WebSocket/Version/RFC6455.php @@ -4,7 +4,7 @@ use Ratchet\Application\WebSocket\Version\RFC6455\HandshakeVerifier; use Guzzle\Http\Message\RequestInterface; /** - * @link http://www.rfc-editor.org/authors/rfc6455.txt + * @link http://tools.ietf.org/html/rfc6455 */ class RFC6455 implements VersionInterface { const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bb897be..e093f23 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,6 +19,7 @@ ./tests/ + ./vendor/ \ No newline at end of file