workerman.php 892 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. use App\Container\Logger;
  4. use App\Vega;
  5. App\Error::register();
  6. $cpuCount = function () {
  7. if (strtolower(PHP_OS) === 'darwin') {
  8. $count = shell_exec('sysctl -n machdep.cpu.core_count');
  9. } else {
  10. $count = shell_exec('nproc');
  11. }
  12. $count = (int)$count > 0 ? (int)$count : 4;
  13. return $count;
  14. };
  15. $vega = Vega::new();
  16. $http = new Workerman\Worker("http://0.0.0.0:2345");
  17. if (\version_compare(\PHP_VERSION, '7.0.0', 'ge') // if php >= 7.0.0
  18. && \version_compare(php_uname('r'), '3.9', 'ge') // if kernel >=3.9
  19. && \strtolower(\php_uname('s')) !== 'darwin' // if not Mac OS
  20. ) { // if not unix socket
  21. $http->reusePort = true;
  22. }
  23. $http->transport = 'tcp';
  24. $http->onMessage = $vega->handler();
  25. $http->count = $cpuCount() * 4;
  26. Logger::instance()->info('Start workerman server');
  27. Workerman\Worker::runAll();