Server.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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 $processes = 4;
  24. /**
  25. * 架构函数
  26. * @access public
  27. */
  28. public function __construct()
  29. {
  30. // 实例化 Websocket 服务
  31. $this->worker = new Worker($this->socket ?: $this->protocol . '://' . $this->host . ':' . $this->port);
  32. // 设置进程数
  33. $this->worker->count = $this->processes;
  34. // 初始化
  35. $this->init();
  36. // 设置回调
  37. foreach (['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerStop', 'onWorkerReload'] as $event) {
  38. if (method_exists($this, $event)) {
  39. $this->worker->$event = [$this, $event];
  40. }
  41. }
  42. // Run worker
  43. Worker::runAll();
  44. }
  45. protected function init()
  46. {
  47. }
  48. }