您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuticateApplication.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Luticate\Utils\Business\LuBusinessException;
  10. use Luticate\Utils\Business\LuLog;
  11. use Luticate\Utils\Dbo\LuDbo;
  12. use Luticate\Utils\Dbo\LuRequestDbo;
  13. use Ratchet\ConnectionInterface;
  14. use Ratchet\MessageComponentInterface;
  15. use Ratchet\Http\HttpServer;
  16. use Ratchet\Server\IoServer;
  17. use Ratchet\WebSocket\WsServer;
  18. class LuticateApplication implements MessageComponentInterface
  19. {
  20. private $_version = 1.0;
  21. private $_config = null;
  22. private $_clients = null;
  23. private $_router = null;
  24. private static $_app = null;
  25. public static function getInstance()
  26. {
  27. return self::$_app;
  28. }
  29. /**
  30. * @param $configuration array
  31. */
  32. public function __construct($configuration)
  33. {
  34. $this->_config = $configuration;
  35. $this->_clients = new \SplObjectStorage;
  36. $this->_router = new LuRoute();
  37. self::$_app = $this;
  38. }
  39. /**
  40. * @return LuRoute
  41. */
  42. public function getRouter()
  43. {
  44. return $this->_router;
  45. }
  46. /**
  47. * @param LuRoute|null $router
  48. */
  49. public function setRouter($router)
  50. {
  51. $this->_router = $router;
  52. }
  53. /**
  54. * @param $version float
  55. */
  56. public function setVersion($version)
  57. {
  58. $this->_version = $version;
  59. }
  60. /**
  61. * @return float
  62. */
  63. public function getVersion()
  64. {
  65. return $this->_version;
  66. }
  67. public function setupRoutes()
  68. {
  69. $app = $this;
  70. require_once __DIR__ . '/../../../../../../app/routes.php';
  71. $this->_router->setup();
  72. }
  73. public function runHttp()
  74. {
  75. $httpMethod = $_SERVER['REQUEST_METHOD'];
  76. $url = $_SERVER['REQUEST_URI'];
  77. $parameters = array_merge($_GET, $_POST);
  78. $r = new LuRequestDbo();
  79. $r->setVersion($this->getVersion());
  80. try {
  81. $result = $this->_router->dispatch($httpMethod, $url, $parameters);
  82. $r->setData($result);
  83. $r->setStatusCode(200);
  84. }
  85. catch (LuBusinessException $e)
  86. {
  87. LuLog::log($e);
  88. $r->setStatusCode($e->getCode());
  89. $r->setMessage($e->getMessage());
  90. }
  91. catch (\Exception $e)
  92. {
  93. LuLog::log($e);
  94. $r->setStatusCode(500);
  95. $r->setMessage("Internal Error");
  96. }
  97. http_response_code($r->getStatusCode());
  98. echo $r->__toString();
  99. }
  100. public function runWs()
  101. {
  102. $server = IoServer::factory(
  103. new HttpServer(
  104. new WsServer(
  105. $this
  106. )
  107. ),
  108. $this->_config['websocket']['port'],
  109. $this->_config['websocket']['address']
  110. );
  111. $server->run();
  112. }
  113. function onMessage(ConnectionInterface $from, $msg)
  114. {
  115. // TODO: Implement onMessage() method.
  116. }
  117. function onOpen(ConnectionInterface $conn)
  118. {
  119. $this->_clients->attach($conn);
  120. }
  121. function onClose(ConnectionInterface $conn)
  122. {
  123. $this->_clients->detach($conn);
  124. }
  125. function onError(ConnectionInterface $conn, \Exception $e)
  126. {
  127. $conn->close();
  128. }
  129. }