Init WAMP
Starting work on the WebSocket Application Messaging Protocol (WAMP). JSON messages, supports pub/sub and RPC via HTTP endpoints for website integration
This commit is contained in:
parent
8d1b2548e7
commit
c56ba3b2f1
89
lib/Ratchet/Application/WAMP/App.php
Normal file
89
lib/Ratchet/Application/WAMP/App.php
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
<?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) {
|
||||||
|
}
|
||||||
|
}
|
5
lib/Ratchet/Application/WAMP/Exception.php
Normal file
5
lib/Ratchet/Application/WAMP/Exception.php
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Application\WAMP;
|
||||||
|
|
||||||
|
class Exception extends \Exception {
|
||||||
|
}
|
31
lib/Ratchet/Application/WAMP/JSONException.php
Normal file
31
lib/Ratchet/Application/WAMP/JSONException.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Application\WAMP;
|
||||||
|
|
||||||
|
class JSONException extends \Exception {
|
||||||
|
public function __construct() {
|
||||||
|
$code = json_last_error();
|
||||||
|
|
||||||
|
switch ($code) {
|
||||||
|
case JSON_ERROR_DEPTH:
|
||||||
|
$msg = 'Maximum stack depth exceeded';
|
||||||
|
break;
|
||||||
|
case JSON_ERROR_STATE_MISMATCH:
|
||||||
|
$msg = 'Underflow or the modes mismatch';
|
||||||
|
break;
|
||||||
|
case JSON_ERROR_CTRL_CHAR:
|
||||||
|
$msg = 'Unexpected control character found';
|
||||||
|
break;
|
||||||
|
case JSON_ERROR_SYNTAX:
|
||||||
|
$msg = 'Syntax error, malformed JSON';
|
||||||
|
break;
|
||||||
|
case JSON_ERROR_UTF8:
|
||||||
|
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$msg = 'Unknown error';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
parent::__construct($msg, $code);
|
||||||
|
}
|
||||||
|
}
|
13
lib/Ratchet/Application/WAMP/ServerInterface.php
Normal file
13
lib/Ratchet/Application/WAMP/ServerInterface.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Application\WAMP;
|
||||||
|
use Ratchet\Resource\Connection;
|
||||||
|
|
||||||
|
interface ServerInterface {
|
||||||
|
function onCall(Connection $conn, $callID, $uri);
|
||||||
|
|
||||||
|
function onSubscribe(Connection $conn, $uri);
|
||||||
|
|
||||||
|
function onUnSubscribe(Connection $conn, $uri);
|
||||||
|
|
||||||
|
function onPublish(Connection $conn, $uri, $event);
|
||||||
|
}
|
@ -76,6 +76,7 @@ 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,6 +93,15 @@ 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) {
|
||||||
|
// 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 = '';
|
$header = '';
|
||||||
foreach ($response as $key => $val) {
|
foreach ($response as $key => $val) {
|
||||||
if (!empty($key)) {
|
if (!empty($key)) {
|
||||||
|
@ -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
|
* Currently instead of this, I'm setting header in the Connection object passed around...not sure which I like more
|
||||||
* @param string
|
* @param string
|
||||||
*/
|
*/
|
||||||
function setHeaders($headers);
|
//function setHeaders($headers);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
|
Loading…
Reference in New Issue
Block a user