use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use ExHttp\Server as ConnectionHandler;
use ExDraw\Server as DrawServer;
use ExHttp\Router;
use ExHttp\StaticFile;
$loop = new React\EventLoop\ExtEventLoop();
$documentRoot = getcwd();
$drawServer = new DrawServer();
$clientBootstrap = file_get_contents(__DIR__ . '/index.html');
/*
   Configure router, the router allows for callbacks to be executed on a route match.  If no
   match is found then it will look to see if it can load a static file.
*/
$router = new ExHttp\Router(array("/" => function ($request, $response, $connection) use($clientBootstrap) {
    $response->setBody($clientBootstrap);
    $connection->send($response);
    $connection->close();
}));
$fileHandler = new StaticFile($documentRoot, $loop);
$router->setFileHandler($fileHandler);
/*
    Websocket and HTTP Server Sockets
*/
$socketServer = new React\Socket\Server($loop);
$socketServer->listen('8080', '0.0.0.0');
$websocketServer = new IoServer(new HttpServer(new WsServer($drawServer)), $socketServer, $loop);
$httpSocket = new React\Socket\Server($loop);
$httpSocket->listen('81', '0.0.0.0');
$httpServer = new IoServer(new HttpServer(new ConnectionHandler($router)), $httpSocket, $loop);
$loop->run();
use SlideBuilder\Server as SlideServer;
use SlideBuilder\Builder as SlideBuilder;
use ExHttp\Router;
use ExHttp\StaticFile;
use EventConfig as EventBaseConfig;
use ExHttp\SseMessage as EventMessage;
$cfg = new \EventConfig();
$cfg->requireFeatures(EventBaseConfig::FEATURE_FDS);
$loop = new React\EventLoop\ExtEventLoop($cfg);
$documentRoot = getcwd();
/* 
	Configure router, the router allows for callbacks to be executed on a route match.  If no
	match is found then it will look to see if it can load a static file.
*/
$connections = [];
$router = new ExHttp\Router();
$connectionHandler = new ConnectionHandler($router);
$fileHandler = new StaticFile($documentRoot, $loop);
$router->setFileHandler($fileHandler);
$connectionHandler->on('close', function ($conn) use(&$connections) {
    foreach ($connections as $key => $data) {
        $connection = $data['conn'];
        if ($connection === $conn) {
            unset($connections[$key]);
            break;
        }
    }
});
$loop->addPeriodicTimer(1, function ($timer) use(&$connections) {
    foreach ($connections as $data) {
        echo get_class($data['conn']) . PHP_EOL;