From 8b3591ce0d03c3ab325d73c353e055f6711d9f15 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Wed, 8 May 2013 20:42:49 -0400 Subject: [PATCH] App class --- CHANGELOG.md | 4 +- README.md | 13 ++-- composer.lock | 1 + src/Ratchet/App.php | 96 +++++++++++++++++++++++++++ src/Ratchet/Http/RoutedHttpServer.php | 65 ------------------ 5 files changed, 104 insertions(+), 75 deletions(-) create mode 100644 src/Ratchet/App.php delete mode 100644 src/Ratchet/Http/RoutedHttpServer.php diff --git a/CHANGELOG.md b/CHANGELOG.md index cada583..aac7b5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ CHANGELOG * 0.3.0 (2013-xx-xx) + * Sugar and spice and everything nice: Added the Ratchet\App class for ease of use * Added Symfony/2.2 based HTTP Router component to allowing for a single Ratchet server to handle multiple apps -> Ratchet\Http\Router * BC: Decoupled HTTP from WebSocket component -> Ratchet\Http\HttpServer * Updated dependency to React/0.3 @@ -79,4 +80,5 @@ CHANGELOG * 0.1 (2012-05-11) * First release with components: IoServer, WsServer, SessionProvider, WampServer, FlashPolicy, IpBlackList - * I/O now handled by React, making Ratchet fully asynchronous \ No newline at end of file + * I/O now handled by React, making Ratchet fully asynchronous + diff --git a/README.md b/README.md index ca1a448..767e45c 100644 --- a/README.md +++ b/README.md @@ -36,9 +36,6 @@ Need help? Have a question? Want to provide feedback? Write a message on the addRoute('/echo', new AbFuzzyServer); - $router->addRoute('/chat', new Chat); - - $server = IoServer::factory($router, 8000); - $server->run(); + $app = new Ratchet\App('example.com', 8080); + $app->route('/chat', new Chat); + $app->route('/echo', new Ratchet\Server\EchoServer); + $app->run(); ``` $ php chat.php \ No newline at end of file diff --git a/composer.lock b/composer.lock index e12161a..f49c13e 100644 --- a/composer.lock +++ b/composer.lock @@ -28,6 +28,7 @@ "Evenement": "src" } }, + "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php new file mode 100644 index 0000000..2aeca35 --- /dev/null +++ b/src/Ratchet/App.php @@ -0,0 +1,96 @@ +listen($port, $address); + + $this->routes = new RouteCollection; + $this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop); + + $policy = new FlashPolicy; + $policy->addAllowedAccess($httpHost, 80); + $policy->addAllowedAccess($httpHost, $port); + $flashSock = new Reactor($loop); + $this->flashServer = new IoServer($policy, $flashSock); + + if (80 == $port) { + $flashSock->listen(843, '0.0.0.0'); + } else { + $flashSock->listen(8843); + } + } + + /** + * @param string $path + * @param ComponentInterface $controller + * @return ComponentInterface + */ + public function route($path, ComponentInterface $controller) { + if ($controller instanceof HttpServerInterface || $controller instanceof WsServer) { + $decorated = $controller; + } elseif ($controller instanceof WampServerInterface) { + $decorated = new WsServer(new WampServer($controller)); + } elseif ($controller instanceof MessageComponentInterface) { + $decorated = new WsServer($controller); + } else { + $decorated = $controller; + } + + $this->routes->add(uniqid(), new Route($path, array('_controller' => $decorated))); + + return $decorated; + } + + public function run() { + $this->_server->run(); + } +} \ No newline at end of file diff --git a/src/Ratchet/Http/RoutedHttpServer.php b/src/Ratchet/Http/RoutedHttpServer.php deleted file mode 100644 index 728424c..0000000 --- a/src/Ratchet/Http/RoutedHttpServer.php +++ /dev/null @@ -1,65 +0,0 @@ -_routes = $routes; - $this->_server = new HttpServer(new Router(new UrlMatcher($routes, new RequestContext))); - } - - public function addRoute($path, MessageComponentInterface $controller) { - $this->_routes->add(uniqid(), new Route($path, array( - '_controller' => new WsServer($controller) - ))); - } - - public function addHttpRoute($path, HttpServerInterface $controller) { - $this->_routes->add(uniqid(), new Route($path, array( - '_controller' => $controller - ))); - } - - /** - * {@inheritdoc} - */ - function onOpen(ConnectionInterface $conn) { - $this->_server->onOpen($conn); - } - - /** - * {@inheritdoc} - */ - function onMessage(ConnectionInterface $from, $msg) { - $this->_server->onMessage($from, $msg); - } - - /** - * {@inheritdoc} - */ - function onClose(ConnectionInterface $conn) { - $this->_server->onClose($conn); - } - - /** - * {@inheritdoc} - */ - function onError(ConnectionInterface $conn, \Exception $e) { - $this->_server->onError($conn, $e); - } -} \ No newline at end of file