eof_server.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. 'worker_num' => 1, //how much worker will start
  14. 'open_eof_split' => true,
  15. 'package_eof' => "\r\n",
  16. 'package_max_length' => 8 * 1024 * 1024,
  17. ));
  18. $this->serv->on('receive', array($this, 'onReceive'));
  19. $this->serv->start();
  20. }
  21. function onReceive($serv, $fd, $tid, $data)
  22. {
  23. echo "recv " . strlen($data) . " bytes\n";
  24. // $packet = substr($data, 4);
  25. // $result = array(
  26. // "code" => "0",
  27. // "msg" => "ok",
  28. // "data" => $packet,
  29. // );
  30. // $resp = json_encode($result);
  31. // $send_data = pack('N', strlen($resp)) . $resp;
  32. // echo "send " . strlen($send_data) . " bytes\n";
  33. // $serv->send($fd, $send_data);
  34. }
  35. }