Server.Class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Mall\Framework\Swoole;
  3. class Server
  4. {
  5. private $moduleServer;
  6. private $moduleData;
  7. private $serviceModuleName;
  8. private $isSetConfig;
  9. function __construct($options = [])
  10. {
  11. class_exists('Swoole_Server') or die("Swoole_Server: check swoole extension");
  12. $this->moduleServer = new \Swoole_Server($options['host'], $options['port']);
  13. if (!isset($options['module']) && $options['module']) {
  14. throw new \Exception('Please define the module name first');
  15. }
  16. $this->serviceModuleName = ucfirst($options['module']);
  17. }
  18. public function setConfig($options = [])
  19. {
  20. $defaultOptions = [
  21. 'worker_num' => 8, // 建议开启核数的1-4倍
  22. 'daemonize' => true,
  23. ];
  24. $options = $options ?: $defaultOptions;
  25. $this->moduleServer->set($options);
  26. $this->isSetConfig = true;
  27. return $this;
  28. }
  29. public function begin()
  30. {
  31. if (!$this->isSetConfig) {
  32. throw new \Exception('Please set the configuration file first');
  33. }
  34. $this->registerEvent();
  35. $this->moduleServer->start();
  36. }
  37. private function registerEvent()
  38. {
  39. $this->moduleServer->on('Start', array($this, 'onStart'));
  40. $this->moduleServer->on('Connect', array($this, 'onConnect'));
  41. $this->moduleServer->on('Receive', array($this, 'onReceive'));
  42. $this->moduleServer->on('Close', array($this, 'onClose'));
  43. $this->moduleServer->on('ManagerStart', array($this, 'onManagerStart'));
  44. $this->moduleServer->on('WorkerStart', array($this, 'onWorkerStart'));
  45. $this->moduleServer->on('WorkerStop', array($this, 'onManagerStop'));
  46. }
  47. //启动server时候会触发。
  48. public function onStart($server)
  49. {
  50. echo "Start\n";
  51. }
  52. //client连接成功后触发。
  53. public function onConnect($server, $fd, $from_id)
  54. {
  55. //$a = $serv->send( $fd, "Hello {$fd}!" );
  56. //var_dump($a); //成功返回true
  57. echo '连接时间: '.date('Y-m-d H:i:s').": Client id {$fd}:{$from_id} \n";
  58. }
  59. //接收client发过来的请求
  60. public function onReceive(\swoole_server $server, $fd, $from_id, $moduleData)
  61. {
  62. //$serv->send($fd, $data);
  63. //关闭该work进程
  64. //$serv->stop();
  65. //宕机
  66. //$serv->shutdown();
  67. //$moduleData = str_replace(PACKAGE_EOF, '', $moduleData);
  68. //解析包头
  69. $header = unpack('Nlength/Nuid/Nserid', substr($moduleData, 0, 12));
  70. //错误的包头
  71. if ($header === false)
  72. {
  73. $server->close($fd);
  74. }
  75. $moduleData = substr($moduleData, 12);
  76. echo '获取数据时间: '.date('Y-m-d H:i:s')."Get Message From Client {$fd}:{$moduleData}\n";
  77. if (!($moduleData = json_decode($moduleData, true))) {
  78. throw new \Exception('Please send data to JSON format');
  79. }
  80. $namespace = '\Service\\' . $this->serviceModuleName . '\\Controller\\' . $moduleData['contorller'];
  81. $resource = new $namespace();
  82. if (isset($moduleData['params']) && $moduleData['params']) {
  83. $data = call_user_func([$resource, $moduleData['action']], $moduleData['params']);
  84. } else {
  85. $data = call_user_func([$resource, $moduleData['action']]);
  86. }
  87. $data = pack('NNN', strlen($data), $header['uid'], $header['serid']) . $data;
  88. $result = $server->send($fd, $data);
  89. if(!$result){
  90. throw new \Exception( $this->moduleServer->getLastError());
  91. }
  92. //主动关闭 客户端连接,也会触发onClose事件
  93. $server->close($fd);
  94. //$list = $serv->connection_list();
  95. // foreach ($list as $fd) {
  96. // $serv->send($fd, $data);
  97. // }
  98. }
  99. //客户端断开触发
  100. public function onClose(\swoole_server $server, $fd, $from_id)
  101. {
  102. echo "Client {$fd} close connection\n";
  103. }
  104. public function onManagerStart(\swoole_server $server) {
  105. echo "On manager start.";
  106. }
  107. public function onManagerStop(\swoole_server $server) {
  108. echo "On manager start.";
  109. }
  110. public function onWorkerStart(\swoole_server $server, $workerId) {
  111. echo $workerId . '---';
  112. }
  113. public function getData()
  114. {
  115. return $this->moduleData;
  116. }
  117. }