| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | <?php
/**
 * Created by PhpStorm.
 * User: robin
 * Date: 6/4/16
 * Time: 10:06 PM
 */
namespace Luticate\Utils\Controller;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
class LuticateApplication implements MessageComponentInterface
{
    private $_version = 1.0;
    private $_config = null;
    private $_clients = null;
    
    private static $_app = null;
    
    public static function getInstance()
    {
        return self::$_app;
    }
    /**
     * @param $configuration array
     */
    public function __construct($configuration)
    {
        $this->_config = $configuration;
        $this->_clients = new \SplObjectStorage;
        self::$_app = $this;
    }
    /**
     * @param $version float
     */
    public function setVersion($version)
    {
        $this->_version = $version;
    }
    /**
     * @return float
     */
    public function getVersion()
    {
        return $this->_version;
    }
    
    public function setupRoutes()
    {
        require_once __DIR__ . '/../../../../../../app/routes.php';
        $router = LuRoute::getInstance();
        
        $router->setup();
    }
    
    public function runHttp()
    {
        $httpMethod = $_SERVER['REQUEST_METHOD'];
        $url = $_SERVER['REQUEST_URI'];
        $router = LuRoute::getInstance();
        $parameters = array_merge($_GET, $_POST);
        $router->dispatch($httpMethod, $url, $parameters);
    }
    
    public function runWs()
    {
        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    $this
                )
            ),
            $this->_config['websocket']['port'],
            $this->_config['websocket']['address']
        );
        $server->run();
    }
    function onMessage(ConnectionInterface $from, $msg)
    {
        // TODO: Implement onMessage() method.
    }
    function onOpen(ConnectionInterface $conn)
    {
        $this->_clients->attach($conn);
    }
    function onClose(ConnectionInterface $conn)
    {
        $this->_clients->detach($conn);
    }
    function onError(ConnectionInterface $conn, \Exception $e)
    {
        $conn->close();
    }
}
 |