App class
This commit is contained in:
parent
3c54fc4bfd
commit
8b3591ce0d
@ -10,6 +10,7 @@ CHANGELOG
|
|||||||
|
|
||||||
* 0.3.0 (2013-xx-xx)
|
* 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
|
* 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
|
* BC: Decoupled HTTP from WebSocket component -> Ratchet\Http\HttpServer
|
||||||
* Updated dependency to React/0.3
|
* Updated dependency to React/0.3
|
||||||
@ -80,3 +81,4 @@ CHANGELOG
|
|||||||
|
|
||||||
* First release with components: IoServer, WsServer, SessionProvider, WampServer, FlashPolicy, IpBlackList
|
* First release with components: IoServer, WsServer, SessionProvider, WampServer, FlashPolicy, IpBlackList
|
||||||
* I/O now handled by React, making Ratchet fully asynchronous
|
* I/O now handled by React, making Ratchet fully asynchronous
|
||||||
|
|
||||||
|
13
README.md
13
README.md
@ -36,9 +36,6 @@ 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\Tests\AbFuzzyServer;
|
|
||||||
|
|
||||||
require __DIR__ . '/vendor/autoload.php';
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
@ -75,12 +72,10 @@ 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
|
||||||
$router = new RoutedHttpServer;
|
$app = new Ratchet\App('example.com', 8080);
|
||||||
$router->addRoute('/echo', new AbFuzzyServer);
|
$app->route('/chat', new Chat);
|
||||||
$router->addRoute('/chat', new Chat);
|
$app->route('/echo', new Ratchet\Server\EchoServer);
|
||||||
|
$app->run();
|
||||||
$server = IoServer::factory($router, 8000);
|
|
||||||
$server->run();
|
|
||||||
```
|
```
|
||||||
|
|
||||||
$ php chat.php
|
$ php chat.php
|
1
composer.lock
generated
1
composer.lock
generated
@ -28,6 +28,7 @@
|
|||||||
"Evenement": "src"
|
"Evenement": "src"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
"license": [
|
"license": [
|
||||||
"MIT"
|
"MIT"
|
||||||
],
|
],
|
||||||
|
96
src/Ratchet/App.php
Normal file
96
src/Ratchet/App.php
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet;
|
||||||
|
use React\EventLoop\LoopInterface;
|
||||||
|
use React\EventLoop\Factory as LoopFactory;
|
||||||
|
use React\Socket\Server as Reactor;
|
||||||
|
use Ratchet\Http\HttpServerInterface;
|
||||||
|
use Ratchet\Wamp\WampServerInterface;
|
||||||
|
use Ratchet\Server\IoServer;
|
||||||
|
use Ratchet\Server\FlashPolicy;
|
||||||
|
use Ratchet\Http\HttpServer;
|
||||||
|
use Ratchet\Http\Router;
|
||||||
|
use Ratchet\WebSocket\WsServer;
|
||||||
|
use Ratchet\Wamp\WampServer;
|
||||||
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
use Symfony\Component\Routing\Route;
|
||||||
|
use Symfony\Component\Routing\RequestContext;
|
||||||
|
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo Security - same origin by default
|
||||||
|
*/
|
||||||
|
class App {
|
||||||
|
/**
|
||||||
|
* @var \Symfony\Component\Routing\RouteCollection
|
||||||
|
*/
|
||||||
|
public $routes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Ratchet\Server\IoServer
|
||||||
|
*/
|
||||||
|
public $flashServer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Ratchet\Server\IoServer
|
||||||
|
*/
|
||||||
|
protected $_server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $httpHost
|
||||||
|
* @param int $port
|
||||||
|
* @param string $address
|
||||||
|
* @param LoopInterface $loop
|
||||||
|
*/
|
||||||
|
public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) {
|
||||||
|
if (extension_loaded('xdebug')) {
|
||||||
|
echo "XDebug extension detected. Remember to disable this if performance testing or going live!\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $loop) {
|
||||||
|
$loop = LoopFactory::create();
|
||||||
|
}
|
||||||
|
|
||||||
|
$socket = new Reactor($loop);
|
||||||
|
$socket->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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,65 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user