API docs on new features

This commit is contained in:
Chris Boden 2013-07-22 21:03:19 -04:00
parent 55c880a100
commit 4039a643ea
3 changed files with 29 additions and 8 deletions

View File

@ -17,6 +17,10 @@ use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher;
/**
* An opinionated facade class to quickly and easily create a WebSocket server.
* A few configuration assumptions are made and some best-practice security conventions are applied by default.
*/
class App { class App {
/** /**
* @var \Symfony\Component\Routing\RouteCollection * @var \Symfony\Component\Routing\RouteCollection
@ -39,13 +43,16 @@ class App {
*/ */
protected $httpHost; protected $httpHost;
/**
* @var int
*/
protected $_routeCounter = 0; protected $_routeCounter = 0;
/** /**
* @param string $httpHost * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');`
* @param int $port * @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843
* @param string $address * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine.
* @param LoopInterface $loop * @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you.
*/ */
public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) { public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) {
if (extension_loaded('xdebug')) { if (extension_loaded('xdebug')) {
@ -78,8 +85,9 @@ class App {
} }
/** /**
* @param $path * Add an endpiont/application to the server
* @param ComponentInterface $controller * @param string $path The URI the client will connect to
* @param ComponentInterface $controller Your application to server for the route. If not specified, assumed to be for a WebSocket
* @param array $allowedOrigins An array of hosts allowed to connect (same host by default), [*] for any * @param array $allowedOrigins An array of hosts allowed to connect (same host by default), [*] for any
* @param string $httpHost Override the $httpHost variable provided in the __construct * @param string $httpHost Override the $httpHost variable provided in the __construct
* @return ComponentInterface|WsServer * @return ComponentInterface|WsServer
@ -110,6 +118,9 @@ class App {
return $decorated; return $decorated;
} }
/**
* Run the server by entering the event loop
*/
public function run() { public function run() {
$this->_server->run(); $this->_server->run();
} }

View File

@ -5,6 +5,11 @@ use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface; use Ratchet\MessageComponentInterface;
use Guzzle\Http\Message\Response; use Guzzle\Http\Message\Response;
/**
* A middleware to ensure JavaScript clients connecting are from the expected domain.
* This protects other websites from open WebSocket connections to your application.
* Note: This can be spoofed from non-web browser clients
*/
class OriginCheck implements HttpServerInterface { class OriginCheck implements HttpServerInterface {
/** /**
* @var \Ratchet\MessageComponentInterface * @var \Ratchet\MessageComponentInterface
@ -13,6 +18,10 @@ class OriginCheck implements HttpServerInterface {
public $allowedOrigins = array(); public $allowedOrigins = array();
/**
* @param MessageComponentInterface $component Component/Application to decorate
* @param array $allowed An array of allowed domains that are allowed to connect from
*/
public function __construct(MessageComponentInterface $component, array $allowed = array()) { public function __construct(MessageComponentInterface $component, array $allowed = array()) {
$this->_component = $component; $this->_component = $component;
$this->allowedOrigins += $allowed; $this->allowedOrigins += $allowed;

View File

@ -3,6 +3,9 @@ namespace Ratchet\Server;
use Ratchet\MessageComponentInterface; use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
/**
* A simple Ratchet application that will reply to all messages with the message it received
*/
class EchoServer implements MessageComponentInterface { class EchoServer implements MessageComponentInterface {
public function onOpen(ConnectionInterface $conn) { public function onOpen(ConnectionInterface $conn) {
} }
@ -15,8 +18,6 @@ class EchoServer implements MessageComponentInterface {
} }
public function onError(ConnectionInterface $conn, \Exception $e) { public function onError(ConnectionInterface $conn, \Exception $e) {
echo $e->getMessage() . "\n";
$conn->close(); $conn->close();
} }
} }