diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 3d8dec0..cc44694 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -17,6 +17,10 @@ use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RequestContext; 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 { /** * @var \Symfony\Component\Routing\RouteCollection @@ -39,13 +43,16 @@ class App { */ protected $httpHost; + /** + * @var int + */ protected $_routeCounter = 0; /** - * @param string $httpHost - * @param int $port - * @param string $address - * @param LoopInterface $loop + * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');` + * @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 IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine. + * @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) { if (extension_loaded('xdebug')) { @@ -78,8 +85,9 @@ class App { } /** - * @param $path - * @param ComponentInterface $controller + * Add an endpiont/application to the server + * @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 string $httpHost Override the $httpHost variable provided in the __construct * @return ComponentInterface|WsServer @@ -110,6 +118,9 @@ class App { return $decorated; } + /** + * Run the server by entering the event loop + */ public function run() { $this->_server->run(); } diff --git a/src/Ratchet/Http/OriginCheck.php b/src/Ratchet/Http/OriginCheck.php index e043fd8..580935a 100644 --- a/src/Ratchet/Http/OriginCheck.php +++ b/src/Ratchet/Http/OriginCheck.php @@ -5,6 +5,11 @@ use Ratchet\ConnectionInterface; use Ratchet\MessageComponentInterface; 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 { /** * @var \Ratchet\MessageComponentInterface @@ -13,6 +18,10 @@ class OriginCheck implements HttpServerInterface { 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()) { $this->_component = $component; $this->allowedOrigins += $allowed; diff --git a/src/Ratchet/Server/EchoServer.php b/src/Ratchet/Server/EchoServer.php index 3f5069b..4011d66 100644 --- a/src/Ratchet/Server/EchoServer.php +++ b/src/Ratchet/Server/EchoServer.php @@ -3,6 +3,9 @@ namespace Ratchet\Server; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; +/** + * A simple Ratchet application that will reply to all messages with the message it received + */ class EchoServer implements MessageComponentInterface { public function onOpen(ConnectionInterface $conn) { } @@ -15,8 +18,6 @@ class EchoServer implements MessageComponentInterface { } public function onError(ConnectionInterface $conn, \Exception $e) { - echo $e->getMessage() . "\n"; - $conn->close(); } } \ No newline at end of file