WAMP incoming functionality

WAMP server interface initial incoming message functionality
Able to receive and pass on client to server messages to attached interface
This commit is contained in:
Chris Boden 2012-01-13 23:46:30 -05:00
parent 34d2f801c8
commit ad302a00d1
5 changed files with 99 additions and 33 deletions

View File

@ -19,13 +19,10 @@ use Ratchet\Resource\Connection;
* | EVENT | 8 | Server-to-Client | * | EVENT | 8 | Server-to-Client |
* +--------------+----+------------------+ * +--------------+----+------------------+
* @link http://www.tavendo.de/autobahn/protocol.html * @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 { class App implements WebSocketAppInterface {
protected $_app; protected $_app;
protected static $_incoming = array(1, 2, 5, 6, 7);
public function getSubProtocol() { public function getSubProtocol() {
return 'wamp'; return 'wamp';
} }
@ -42,16 +39,12 @@ class App implements WebSocketAppInterface {
$conn->prefixes[$uri] = $curie; $conn->prefixes[$uri] = $curie;
} }
public function sendEvent($uri, $event) {
}
public function onCall(Connection $conn, $id, $uri) {
}
public function onOpen(Connection $conn) { public function onOpen(Connection $conn) {
$conn->WAMP = new \StdClass; $conn->WAMP = new \StdClass;
$conn->WAMP->prefixes = array(); $conn->WAMP->prefixes = array();
$conn->WAMP->subscriptions = array(); $conn->WAMP->subscriptions = array();
return $this->_app->onOpen($conn);
} }
/** /**
@ -64,15 +57,35 @@ class App implements WebSocketAppInterface {
throw new JSONException; throw new JSONException;
} }
if (!in_array($json[0], static::$_incoming)) { 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'); throw new Exception('Invalid message type');
} }
if ($json[0] == 1) { // create method to loop through $ret
$this->addPrefix($conn, $json[2], $json[1]); // json_encode messages, return $ret back to WebSocket
}
// Determine WAMP message type, call $_this->_app->on(); return $ret;
} }
public function __construct(ServerInterface $app) { public function __construct(ServerInterface $app) {
@ -80,9 +93,10 @@ class App implements WebSocketAppInterface {
} }
public function onClose(Connection $conn) { 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) { public function onError(Connection $conn, \Exception $e) {
return $this->_app->onError($conn, $e);
} }
} }

View File

@ -2,12 +2,63 @@
namespace Ratchet\Application\WAMP; namespace Ratchet\Application\WAMP;
use Ratchet\Resource\Connection; 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 { 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); 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); 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); 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); function onPublish(Connection $conn, $uri, $event);
} }

View File

@ -42,6 +42,12 @@ class App implements ApplicationInterface, ConfiguratorInterface {
protected $_mask_payload = false; protected $_mask_payload = false;
/**
* @deprecated
* @temporary
*/
public $accepted_subprotocols = array();
public function __construct(ApplicationInterface $app) { public function __construct(ApplicationInterface $app) {
$this->_app = $app; $this->_app = $app;
$this->_factory = new Factory; $this->_factory = new Factory;
@ -73,7 +79,6 @@ class App implements ApplicationInterface, ConfiguratorInterface {
* Do handshake, frame/unframe messages coming/going in stack * Do handshake, frame/unframe messages coming/going in stack
* @todo This needs some major refactoring * @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 "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) { public function onMessage(Connection $from, $msg) {
if (true !== $from->WebSocket->handshake) { if (true !== $from->WebSocket->handshake) {
@ -92,13 +97,16 @@ class App implements ApplicationInterface, ConfiguratorInterface {
$from->WebSocket->handshake = true; $from->WebSocket->handshake = true;
if (is_array($response)) { if (is_array($response)) {
if ($this->_app instanceof WebSocketAppInterface) { // This block is to be moved/changed later
// Note: this logic is wrong - we're supposed to check what the client sent $agreed_protocols = array();
// as its sub protocol and if we support any of the requested we send that back. $requested_protocols = $from->WebSocket->headers->getTokenizedHeader('Sec-WebSocket-Protocol', ',');
// This is just sending what ever one wwe support foreach ($this->accepted_subprotocols as $sub_protocol) {
// This will be changed when I rewrite how headers are handled if (false !== $requested_protocols->hasValue($sub_protocol)) {
// Also...what happens if something like a logger is put between this and the sub-protocol app? $agreed_protocols[] = $sub_protocol;
$response['Sec-WebSocket-Protocol'] = $this->_app->getSubProtocol(); }
}
if (count($agreed_protocols) > 0) {
$response['Sec-WebSocket-Protocol'] = implode(',', $agreed_protocols);
} }
$header = ''; $header = '';
@ -163,14 +171,6 @@ class App implements ApplicationInterface, ConfiguratorInterface {
return $this->_app->onError($conn, $e); 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 * Checks if a return Command from your application is a message, if so encode it/them
* @param Ratchet\Resource\Command\CommandInterface|NULL * @param Ratchet\Resource\Command\CommandInterface|NULL

View File

@ -4,7 +4,7 @@ use Ratchet\Application\WebSocket\Version\RFC6455\HandshakeVerifier;
use Guzzle\Http\Message\RequestInterface; 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 { class RFC6455 implements VersionInterface {
const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';

View File

@ -19,6 +19,7 @@
<filter> <filter>
<blacklist> <blacklist>
<directory>./tests/</directory> <directory>./tests/</directory>
<directory>./vendor/</directory>
</blacklist> </blacklist>
</filter> </filter>
</phpunit> </phpunit>