length_server.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. $serv = new SocketServer();
  3. $serv->run('0.0.0.0', 9504);
  4. class SocketServer
  5. {
  6. protected $serv; //swoole server
  7. const MAX_PACKAGE_LEN = 8000000; //max data accept
  8. function run($host, $port)
  9. {
  10. $this->serv = new swoole_server($host, $port, SWOOLE_BASE);
  11. $this->serv->set(array(
  12. 'enable_coroutine' => false,
  13. // 'dispatch_mode' => 3, //who come first who is
  14. 'worker_num' => 1, //how much worker will start
  15. 'open_length_check' => true,
  16. 'package_max_length' => 8 * 1024 * 1024,
  17. 'package_length_type' => 'N', //see php pack()
  18. 'package_length_offset' => 0,
  19. 'package_body_offset' => 4,
  20. ));
  21. $this->serv->on('receive', array($this, 'onReceive'));
  22. $this->serv->start();
  23. }
  24. function onReceive($serv, $fd, $tid, $data)
  25. {
  26. echo "recv " . strlen($data) . " bytes\n";
  27. // $packet = substr($data, 4);
  28. // $result = array(
  29. // "code" => "0",
  30. // "msg" => "ok",
  31. // "data" => $packet,
  32. // );
  33. // $resp = json_encode($result);
  34. // $send_data = pack('N', strlen($resp)) . $resp;
  35. // echo "send " . strlen($send_data) . " bytes\n";
  36. // $serv->send($fd, $send_data);
  37. }
  38. }