Server.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\worker;
  12. use Workerman\Worker;
  13. /**
  14. * Worker控制器扩展类
  15. */
  16. abstract class Server
  17. {
  18. protected $worker;
  19. protected $socket = '';
  20. protected $protocol = 'http';
  21. protected $host = '0.0.0.0';
  22. protected $port = '2346';
  23. protected $option = [];
  24. protected $context = [];
  25. protected $event = ['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerReload', 'onWebSocketConnect'];
  26. /**
  27. * 架构函数
  28. * @access public
  29. */
  30. public function __construct()
  31. {
  32. // 实例化 Websocket 服务
  33. $this->worker = new Worker($this->socket ?: $this->protocol . '://' . $this->host . ':' . $this->port, $this->context);
  34. // 设置参数
  35. if (!empty($this->option)) {
  36. foreach ($this->option as $key => $val) {
  37. $this->worker->$key = $val;
  38. }
  39. }
  40. // 设置回调
  41. foreach ($this->event as $event) {
  42. if (method_exists($this, $event)) {
  43. $this->worker->$event = [$this, $event];
  44. }
  45. }
  46. // 初始化
  47. $this->init();
  48. }
  49. protected function init()
  50. {
  51. }
  52. public function start()
  53. {
  54. Worker::runAll();
  55. }
  56. public function __set($name, $value)
  57. {
  58. $this->worker->$name = $value;
  59. }
  60. public function __call($method, $args)
  61. {
  62. call_user_func_array([$this->worker, $method], $args);
  63. }
  64. }