[Http] Facade class for routing

Not sure if it'll stick or not, but easier way to route for now
Refs #15
This commit is contained in:
Chris Boden 2013-04-27 00:05:10 -04:00
parent 4df71c3a35
commit 26a760709f
2 changed files with 71 additions and 13 deletions

View File

@ -38,15 +38,9 @@ Need help? Have a question? Want to provide feedback? Write a message on the
<?php <?php
use Ratchet\MessageComponentInterface; use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\Http\RoutedHttpServer;
use Ratchet\Server\IoServer; use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer; use Ratchet\Tests\AbFuzzyServer;
use Ratchet\Http\Router;
use Ratchet\WebSocket\WsServer;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
@ -83,12 +77,11 @@ class Chat implements MessageComponentInterface {
} }
// Run the server application through the WebSocket protocol on port 8080 // Run the server application through the WebSocket protocol on port 8080
// This will be made easier soon, routing currently in development $router = new RoutedHttpServer;
$routes->add('chatRoute', new Route('/chat', array( $router->addRoute('/echo', new AbFuzzyServer);
'_controller' => new WsServer(new Chat) $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(); $server->run();
``` ```

View File

@ -0,0 +1,65 @@
<?php
namespace Ratchet\Http;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\WebSocket\WsServer;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
/**
*/
class RoutedHttpServer implements MessageComponentInterface {
protected $_routes;
protected $_server;
public function __construct(RouteCollection $routes = null) {
if (null == $routes) {
$routes = new RouteCollection;
}
$this->_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);
}
}