[WAMP] Welcome message

This commit is contained in:
Chris Boden 2012-04-26 19:49:49 -04:00
parent 9d071df140
commit daf8731790
2 changed files with 29 additions and 4 deletions

View File

@ -4,9 +4,14 @@ use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Component\WAMP\WAMPServerComponent as WAMP; use Ratchet\Component\WAMP\WAMPServerComponent as WAMP;
/** /**
* @todo Needs work - sessionId needs to be stored w/ the Connection object * Send Welcome message to each new connecting client
*/ */
class CallResult extends SendMessage { class Welcome extends SendMessage {
/**
* @param string The unique identifier to mark the client
* @param string The server application name/version
* @return Welcome
*/
public function setWelcome($sessionId, $serverIdent = '') { public function setWelcome($sessionId, $serverIdent = '') {
return $this->setMessage(json_encode(array(WAMP::MSG_WELCOME, $sessionId, 1, $serverIdent))); return $this->setMessage(json_encode(array(WAMP::MSG_WELCOME, $sessionId, 1, $serverIdent)));
} }

View File

@ -5,6 +5,7 @@ use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Command\Composite; use Ratchet\Resource\Command\Composite;
use Ratchet\Resource\Command\CommandInterface; use Ratchet\Resource\Command\CommandInterface;
use Ratchet\Component\WAMP\Command\Action\Prefix; use Ratchet\Component\WAMP\Command\Action\Prefix;
use Ratchet\Component\WAMP\Command\Action\Welcome;
/** /**
* WebSocket Application Messaging Protocol * WebSocket Application Messaging Protocol
@ -45,6 +46,9 @@ class WAMPServerComponent implements WebSocketComponentInterface {
*/ */
protected $_msg_buffer = null; protected $_msg_buffer = null;
/**
* {@inheritdoc}
*/
public function getSubProtocol() { public function getSubProtocol() {
return 'wamp'; return 'wamp';
} }
@ -68,6 +72,9 @@ class WAMPServerComponent implements WebSocketComponentInterface {
} }
} }
/**
* {@inheritdoc}
*/
public function onOpen(ConnectionInterface $conn) { public function onOpen(ConnectionInterface $conn) {
$conn->WAMP = new \StdClass; $conn->WAMP = new \StdClass;
$conn->WAMP->prefixes = array(); $conn->WAMP->prefixes = array();
@ -78,11 +85,15 @@ class WAMPServerComponent implements WebSocketComponentInterface {
$wamp->addPrefix($conn, $curie, $uri, true); $wamp->addPrefix($conn, $curie, $uri, true);
}; };
return $this->_decorating->onOpen($conn); $welcome = new Welcome($conn);
$welcome->setWelcome(uniqid(), 'Ratchet/0.1');
$this->_msg_buffer->enqueue($welcome);
return $this->attachStack($this->_decorating->onOpen($conn));
} }
/** /**
* @{inherit} * @{inheritdoc}
* @throws Exception * @throws Exception
* @throws JSONException * @throws JSONException
*/ */
@ -152,15 +163,24 @@ class WAMPServerComponent implements WebSocketComponentInterface {
return $stack; return $stack;
} }
/**
* @param WAMPServerComponentInterface An class to propagate calls through
*/
public function __construct(WAMPServerComponentInterface $server_component) { public function __construct(WAMPServerComponentInterface $server_component) {
$this->_decorating = $server_component; $this->_decorating = $server_component;
$this->_msg_buffer = new Composite; $this->_msg_buffer = new Composite;
} }
/**
* {@inheritdoc}
*/
public function onClose(ConnectionInterface $conn) { public function onClose(ConnectionInterface $conn) {
return $this->_decorating->onClose($conn); return $this->_decorating->onClose($conn);
} }
/**
* {@inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) { public function onError(ConnectionInterface $conn, \Exception $e) {
return $this->_decorating->onError($conn, $e); return $this->_decorating->onError($conn, $e);
} }