server.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. use Hyperf\Server\Event;
  12. use Hyperf\Server\Server;
  13. use Swoole\Constant;
  14. return [
  15. 'mode' => SWOOLE_PROCESS,
  16. 'servers' => [
  17. [
  18. 'name' => 'http',
  19. 'type' => Server::SERVER_HTTP,
  20. 'host' => '0.0.0.0',
  21. 'port' => 9101,
  22. 'sock_type' => SWOOLE_SOCK_TCP,
  23. 'callbacks' => [
  24. Event::ON_REQUEST => [App\Service\Server\StreamServer::class, 'onRequest'],
  25. // Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
  26. ],
  27. ],
  28. [
  29. 'name' => 'ws',
  30. 'type' => Server::SERVER_WEBSOCKET,
  31. 'host' => '0.0.0.0',
  32. 'port' => 9106,
  33. 'sock_type' => SWOOLE_SOCK_TCP,
  34. 'callbacks' => [
  35. Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
  36. Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
  37. Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
  38. ],
  39. 'settings' => [
  40. // 关键:设置连接最大闲置时间(秒),0 表示永不超时
  41. 'max_idle_time' => 300, // 例如设置为 5 分钟
  42. // 其他可能相关的配置
  43. 'open_tcp_keepalive' => 1, // 开启 TCP 保活机制
  44. 'tcp_keepidle' => 60, // 60 秒内无数据则发送保活探针
  45. 'tcp_keepinterval' => 10, // 探针间隔 10 秒
  46. 'tcp_keepcount' => 3, // 探针失败 3 次后关闭连接
  47. ],
  48. ],
  49. ],
  50. 'settings' => [
  51. 'document_root' => BASE_PATH . '/public',
  52. 'enable_static_handler' => true,
  53. Constant::OPTION_ENABLE_COROUTINE => true,
  54. Constant::OPTION_WORKER_NUM => swoole_cpu_num(),
  55. Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid',
  56. Constant::OPTION_OPEN_TCP_NODELAY => true,
  57. Constant::OPTION_MAX_COROUTINE => 10 * 1024 * 1024,
  58. Constant::OPTION_OPEN_HTTP2_PROTOCOL => true,
  59. Constant::OPTION_MAX_REQUEST => 10 * 1024 * 1024,
  60. Constant::OPTION_SOCKET_BUFFER_SIZE => 10 * 1024 * 1024,
  61. Constant::OPTION_BUFFER_OUTPUT_SIZE => 10 * 1024 * 1024,
  62. Constant::OPTION_UPLOAD_MAX_FILESIZE => 10 * 1024 * 1024,
  63. ],
  64. 'callbacks' => [
  65. Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
  66. Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
  67. Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
  68. ],
  69. ];