func.php 651 B

1234567891011121314151617181920212223242526
  1. <?php
  2. $serv = new swoole_server("127.0.0.1", 9501);
  3. $serv->set(array(
  4. 'open_length_check' => true,
  5. 'dispatch_mode' => 1,
  6. 'package_length_func' => function ($data) {
  7. if (strlen($data) < 8) {
  8. return 0;
  9. }
  10. $length = intval(trim(substr($data, 0, 8)));
  11. if ($length <= 0) {
  12. return -1;
  13. }
  14. return $length + 8;
  15. },
  16. 'package_max_length' => 2000000, //协议最大长度
  17. ));
  18. $serv->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data)
  19. {
  20. var_dump($data);
  21. echo "#{$serv->worker_id}>> received length=" . strlen($data) . "\n";
  22. });
  23. $serv->start();