From 26a760709f24c11e66658e0ef7dea3176f132e13 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sat, 27 Apr 2013 00:05:10 -0400 Subject: [PATCH] [Http] Facade class for routing Not sure if it'll stick or not, but easier way to route for now Refs #15 --- README.md | 19 +++----- src/Ratchet/Http/RoutedHttpServer.php | 65 +++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 src/Ratchet/Http/RoutedHttpServer.php diff --git a/README.md b/README.md index e089404..3f78953 100644 --- a/README.md +++ b/README.md @@ -38,15 +38,9 @@ Need help? Have a question? Want to provide feedback? Write a message on the add('chatRoute', new Route('/chat', array( - '_controller' => new WsServer(new Chat) - ))); + $router = new RoutedHttpServer; + $router->addRoute('/echo', new AbFuzzyServer); + $router->addRoute('/chat', new Chat); - $server = IoServer::factory(new HttpServer(new Router(new UrlMatcher($routes, new RequestContext))), 8080); + $server = IoServer::factory($router, 8000); $server->run(); ``` diff --git a/src/Ratchet/Http/RoutedHttpServer.php b/src/Ratchet/Http/RoutedHttpServer.php new file mode 100644 index 0000000..728424c --- /dev/null +++ b/src/Ratchet/Http/RoutedHttpServer.php @@ -0,0 +1,65 @@ +_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