WAMP server prefix

Server to client set Prefix in WAMP working via lambda in Connection object
This commit is contained in:
Chris Boden 2012-01-18 08:06:02 -05:00
parent dbcb6f2cde
commit f84be39fcf
2 changed files with 81 additions and 7 deletions

View File

@ -3,6 +3,9 @@ namespace Ratchet\Application\WAMP;
use Ratchet\Application\ApplicationInterface; use Ratchet\Application\ApplicationInterface;
use Ratchet\Application\WebSocket\WebSocketAppInterface; use Ratchet\Application\WebSocket\WebSocketAppInterface;
use Ratchet\Resource\Connection; use Ratchet\Resource\Connection;
use Ratchet\Resource\Command\Composite;
use Ratchet\Resource\Command\CommandInterface;
use Ratchet\Application\WAMP\Command\Action\Prefix;
/** /**
* WebSocket Application Messaging Protocol * WebSocket Application Messaging Protocol
@ -24,6 +27,13 @@ use Ratchet\Resource\Connection;
class App implements WebSocketAppInterface { class App implements WebSocketAppInterface {
protected $_app; protected $_app;
/**
* Any server to client prefixes are stored here
* They're taxied along with the next outgoing message
* @var Ratchet\Resource\Command\Composite
*/
protected $_msg_buffer = null;
public function getSubProtocol() { public function getSubProtocol() {
return 'wamp'; return 'wamp';
} }
@ -31,13 +41,20 @@ class App implements WebSocketAppInterface {
/** /**
* @todo WAMP spec does not say what to do when there is an error with PREFIX... * @todo WAMP spec does not say what to do when there is an error with PREFIX...
*/ */
public function addPrefix(Connection $conn, $curie, $uri) { public function addPrefix(Connection $conn, $curie, $uri, $from_server = false) {
// validate uri // validate uri
// validate curie // validate curie
// make sure the curie is shorter than the uri // make sure the curie is shorter than the uri
$conn->WAMP->prefixes[$curie] = $uri; $conn->WAMP->prefixes[$curie] = $uri;
if ($from_server) {
$prefix = new Prefix($conn);
$prefix->setPrefix($curie, $uri);
$this->_msg_buffer->enqueue($prefix);
}
} }
public function onOpen(Connection $conn) { public function onOpen(Connection $conn) {
@ -45,6 +62,11 @@ class App implements WebSocketAppInterface {
$conn->WAMP->prefixes = array(); $conn->WAMP->prefixes = array();
$conn->WAMP->subscriptions = array(); $conn->WAMP->subscriptions = array();
$wamp = $this;
$conn->WAMP->addPrefix = function($curie, $uri) use ($wamp, $conn) {
$wamp->addPrefix($conn, $curie, $uri, true);
};
return $this->_app->onOpen($conn); return $this->_app->onOpen($conn);
} }
@ -60,7 +82,7 @@ class App implements WebSocketAppInterface {
switch ($json[0]) { switch ($json[0]) {
case 1: case 1:
return $this->addPrefix($from, $json[1], $json[2]); $ret = $this->addPrefix($from, $json[1], $json[2]);
break; break;
case 2: case 2:
@ -72,24 +94,26 @@ class App implements WebSocketAppInterface {
$json = $json[0]; $json = $json[0];
} }
return $this->_app->onCall($from, $callID, $procURI, $json); $ret = $this->_app->onCall($from, $callID, $procURI, $json);
break; break;
case 5: case 5:
return $this->_app->onSubscribe($from, $this->getUri($from, $json[1])); $ret = $this->_app->onSubscribe($from, $this->getUri($from, $json[1]));
break; break;
case 6: case 6:
return $this->_app->onUnSubscribe($from, $this->getUri($from, $json[1])); $ret = $this->_app->onUnSubscribe($from, $this->getUri($from, $json[1]));
break; break;
case 7: case 7:
return $this->_app->onPublish($from, $this->getUri($from, $json[1]), $json[2]); $ret = $this->_app->onPublish($from, $this->getUri($from, $json[1]), $json[2]);
break; break;
default: default:
throw new Exception('Invalid message type'); throw new Exception('Invalid message type');
} }
return $this->attachStack($ret);
} }
/** /**
@ -102,8 +126,22 @@ class App implements WebSocketAppInterface {
return (isset($conn->WAMP->prefixes[$uri]) ? $conn->WAMP->prefixes[$uri] : $uri); return (isset($conn->WAMP->prefixes[$uri]) ? $conn->WAMP->prefixes[$uri] : $uri);
} }
/**
* @param Ratchet\Resource\Command\CommandInterface|NULL
* @return Ratchet\Resource\Command\Composite
*/
protected function attachStack(CommandInterface $command = null) {
$stack = $this->_msg_buffer;
$stack->enqueue($command);
$this->_msg_buffer = new Composite;
return $stack;
}
public function __construct(ServerInterface $app) { public function __construct(ServerInterface $app) {
$this->_app = $app; $this->_app = $app;
$this->_msg_buffer = new Composite;
} }
public function onClose(Connection $conn) { public function onClose(Connection $conn) {

View File

@ -0,0 +1,36 @@
<?php
namespace Ratchet\Application\WAMP\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage;
/**
*/
class Prefix extends SendMessage {
protected $_curie;
protected $_uri;
/**
* @param string
* @param string
* @return Prefix
*/
public function setPrefix($curie, $uri) {
$this->_curie = $curie;
$this->_uri = $uri;
return $this->setMessage(json_encode(array(1, $curie, $uri)));
}
/**
* @return string
*/
public function getCurie() {
return $this->_curie;
}
/**
* @return string
*/
public function getUri() {
return $this->_uri;
}
}