From c56ba3b2f1132426ad3e85a16c28b811646e83c2 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 18 Dec 2011 15:32:11 -0500 Subject: [PATCH] Init WAMP Starting work on the WebSocket Application Messaging Protocol (WAMP). JSON messages, supports pub/sub and RPC via HTTP endpoints for website integration --- lib/Ratchet/Application/WAMP/App.php | 89 +++++++++++++++++++ lib/Ratchet/Application/WAMP/Exception.php | 5 ++ .../Application/WAMP/JSONException.php | 31 +++++++ .../Application/WAMP/ServerInterface.php | 13 +++ lib/Ratchet/Application/WebSocket/App.php | 10 +++ .../WebSocket/WebSocketAppInterface.php | 2 +- 6 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 lib/Ratchet/Application/WAMP/App.php create mode 100644 lib/Ratchet/Application/WAMP/Exception.php create mode 100644 lib/Ratchet/Application/WAMP/JSONException.php create mode 100644 lib/Ratchet/Application/WAMP/ServerInterface.php diff --git a/lib/Ratchet/Application/WAMP/App.php b/lib/Ratchet/Application/WAMP/App.php new file mode 100644 index 0000000..cfd320c --- /dev/null +++ b/lib/Ratchet/Application/WAMP/App.php @@ -0,0 +1,89 @@ +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(); + } + + /** + * @{inherit} + * @throws Exception + * @throws JSONException + */ + public function onMessage(Connection $from, $msg) { + if (null === ($json = @json_decode($msg, true))) { + throw new JSONException; + } + + if (!in_array($json[0], static::$_incoming)) { + throw new Exception('Invalid message type'); + } + + if ($json[0] == 1) { + $this->addPrefix($conn, $json[2], $json[1]); + } + } + + public function __construct(ApplicationInterface $app = null) { + if (null !== $app) { + throw new \InvalidArgumentException('WAMP is the end of the Socket stack, apps build on this must conform to the WAMP protocol'); + } + } + + public function onClose(Connection $conn) { + // remove all prefixes associated with connection? or will those just be destroyed w/ Connection + } + + public function onError(Connection $conn, \Exception $e) { + } +} \ No newline at end of file diff --git a/lib/Ratchet/Application/WAMP/Exception.php b/lib/Ratchet/Application/WAMP/Exception.php new file mode 100644 index 0000000..e590104 --- /dev/null +++ b/lib/Ratchet/Application/WAMP/Exception.php @@ -0,0 +1,5 @@ +WebSocket->handshake) { @@ -92,6 +93,15 @@ 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(); + } + $header = ''; foreach ($response as $key => $val) { if (!empty($key)) { diff --git a/lib/Ratchet/Application/WebSocket/WebSocketAppInterface.php b/lib/Ratchet/Application/WebSocket/WebSocketAppInterface.php index f78ca6c..642a625 100644 --- a/lib/Ratchet/Application/WebSocket/WebSocketAppInterface.php +++ b/lib/Ratchet/Application/WebSocket/WebSocketAppInterface.php @@ -12,7 +12,7 @@ interface WebSocketAppInterface extends ApplicationInterface { * Currently instead of this, I'm setting header in the Connection object passed around...not sure which I like more * @param string */ - function setHeaders($headers); + //function setHeaders($headers); /** * @return string