You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuticateApplication.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 6/4/16
  6. * Time: 10:06 PM
  7. */
  8. namespace Luticate\Utils\Controller;
  9. use Ratchet\ConnectionInterface;
  10. use Ratchet\MessageComponentInterface;
  11. use Ratchet\Http\HttpServer;
  12. use Ratchet\Server\IoServer;
  13. use Ratchet\WebSocket\WsServer;
  14. class LuticateApplication implements MessageComponentInterface
  15. {
  16. private $_version = 1.0;
  17. private $_config = null;
  18. private $_clients = null;
  19. private static $_app = null;
  20. public static function getInstance()
  21. {
  22. return self::$_app;
  23. }
  24. /**
  25. * @param $configuration array
  26. */
  27. public function __construct($configuration)
  28. {
  29. $this->_config = $configuration;
  30. $this->_clients = new \SplObjectStorage;
  31. self::$_app = $this;
  32. }
  33. /**
  34. * @param $version float
  35. */
  36. public function setVersion($version)
  37. {
  38. $this->_version = $version;
  39. }
  40. /**
  41. * @return float
  42. */
  43. public function getVersion()
  44. {
  45. return $this->_version;
  46. }
  47. public function setupRoutes()
  48. {
  49. require_once __DIR__ . '/../../../../../../app/routes.php';
  50. $router = LuRoute::getInstance();
  51. $router->setup();
  52. }
  53. public function runHttp()
  54. {
  55. $httpMethod = $_SERVER['REQUEST_METHOD'];
  56. $url = $_SERVER['REQUEST_URI'];
  57. $router = LuRoute::getInstance();
  58. $parameters = array_merge($_GET, $_POST);
  59. $router->dispatch($httpMethod, $url, $parameters);
  60. }
  61. public function runWs()
  62. {
  63. $server = IoServer::factory(
  64. new HttpServer(
  65. new WsServer(
  66. $this
  67. )
  68. ),
  69. $this->_config['websocket']['port'],
  70. $this->_config['websocket']['address']
  71. );
  72. $server->run();
  73. }
  74. function onMessage(ConnectionInterface $from, $msg)
  75. {
  76. // TODO: Implement onMessage() method.
  77. }
  78. function onOpen(ConnectionInterface $conn)
  79. {
  80. $this->_clients->attach($conn);
  81. }
  82. function onClose(ConnectionInterface $conn)
  83. {
  84. $this->_clients->detach($conn);
  85. }
  86. function onError(ConnectionInterface $conn, \Exception $e)
  87. {
  88. $conn->close();
  89. }
  90. }