mxmbsocket/lib/Ratchet/Application/WAMP/App.php
Chris Boden c56ba3b2f1 Init WAMP
Starting work on the WebSocket Application Messaging Protocol (WAMP).
JSON messages, supports pub/sub and RPC via HTTP endpoints for website integration
2011-12-18 15:32:11 -05:00

89 lines
2.7 KiB
PHP

<?php
namespace Ratchet\Application\WAMP;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Application\WebSocket\WebSocketAppInterface;
use Ratchet\Resource\Connection;
/**
* WebSocket Application Messaging Protocol
* +--------------+----+------------------+
* | Message Type | ID | DIRECTION |
* |--------------+----+------------------+
* | PREFIX | 1 | Bi-Directional |
* | CALL | 2 | Client-to-Server |
* | CALL RESULT | 3 | Server-to-Client |
* | CALL ERROR | 4 | Server-to-Client |
* | SUBSCRIBE | 5 | Client-to-Server |
* | UNSUBSCRIBE | 6 | Client-to-Server |
* | PUBLISH | 7 | Client-to-Server |
* | 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 ApplicationInterface, WebSocketAppInterface {
protected static $_incoming = array(1, 2, 5, 6, 7);
public function getSubProtocol() {
return 'wamp';
}
public function attachHandler(ServerInterface $app) {
}
/**
* @todo WAMP spec does not say what to do when there is an error with PREFIX...
*/
public function addPrefix(Connection $conn, $uri, $curie) {
// validate uri
// validate curie
// make sure the curie is shorter than the uri
$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();
}
/**
* @{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) {
}
}