TestHttpServ.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @Author: winterswang
  4. * @Date: 2015-06-18 16:45:09
  5. * @Last Modified by: winterswang
  6. * @Last Modified time: 2016-09-18 17:33:51
  7. */
  8. class TestHttpServer {
  9. public $http;
  10. public $queue;
  11. public $setting = array();
  12. /**
  13. * [__construct description]
  14. * @param array $setting [description]
  15. */
  16. public function __construct(){
  17. }
  18. public function set($setting){
  19. $this ->setting = $setting;
  20. }
  21. /**
  22. * [init description]
  23. * @return [type] [description]
  24. */
  25. public function init(){
  26. if (!isset($this ->setting['host'])) {
  27. $this ->setting['host'] = '0.0.0.0';
  28. }
  29. if (!isset($this ->setting['port'])) {
  30. $this ->setting['port'] = '9999';
  31. }
  32. $this ->http = new swoole_http_server($this ->setting['host'], $this ->setting['port']);
  33. $this ->http ->set($this ->setting);
  34. $this ->http ->on('request', array($this, 'onRequest'));
  35. $this ->http ->on('close', array($this, 'onClose'));
  36. }
  37. /**
  38. * [onRequest description]
  39. * @param [type] $request [description]
  40. * @param [type] $response [description]
  41. * @return [type] [description]
  42. */
  43. public function onRequest($request, $response){
  44. // $udp = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
  45. // $udp->on("connect", function(swoole_client $cli) {
  46. // $cli->send("udp test");
  47. // });
  48. // $udp->on("receive", function(swoole_client $cli, $data)use($response){
  49. $tcp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
  50. $tcp->on("connect", function(swoole_client $cli) {
  51. $cli->send("tcp test");
  52. });
  53. $tcp->on("receive", function(swoole_client $cli, $data)use($response){
  54. $response ->end("<h1> swoole response</h1>");
  55. });
  56. $tcp->on("close", function(swoole_client $cli){
  57. });
  58. $tcp->on("error", function(swoole_client $cli){
  59. });
  60. $tcp->connect('10.100.64.151', 9805);
  61. // });
  62. // $udp->on("close", function(swoole_client $cli){
  63. // });
  64. // $udp->connect('10.100.65.222', 9906);
  65. }
  66. /**
  67. * [onClose description]
  68. * @param [type] $server [description]
  69. * @param [type] $fd [description]
  70. * @param [type] $reactor_id [description]
  71. * @return [type] [description]
  72. */
  73. public function onClose($server, $fd, $reactor_id){
  74. //echo " on close fd = $fd reactor_id = $reactor_id \n";
  75. }
  76. /**
  77. * [start description]
  78. * @return [type] [description]
  79. */
  80. public function start(){
  81. $this ->init();
  82. $this ->http ->start();
  83. }
  84. }
  85. $setting = array(
  86. 'host' => '0.0.0.0',
  87. 'port' => 10005,
  88. 'worker_num' => 4,
  89. 'dispatch_mode' => 2, //固定分配请求到worker
  90. 'reactor_num' => 4, //亲核
  91. 'daemonize' => 1, //守护进程
  92. 'backlog' => 128,
  93. 'log_file' => '/data/log/test_http_server.log',
  94. );
  95. $th = new TestHttpServer();
  96. $th ->set($setting);
  97. $th ->start();