You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?PHP
  2. /**
  3. * struct
  4. * {
  5. * unisgned char type;
  6. * unsigned int time;
  7. * unsigned int serid;
  8. * unsigned int length;
  9. * char body[length];
  10. * }
  11. */
  12. function remoteCall(string $host, $port, string $data) {
  13. $length = strlen($data);
  14. $pack = pack('CNNNa*', 1, time(), 0, $length, $data);
  15. $client = new Swoole\Client(SWOOLE_SOCK_TCP);
  16. $client->set(array(
  17. 'open_length_check' => true,
  18. 'package_max_length' => 81920,
  19. 'package_length_type' => 'N',
  20. 'package_length_offset' => 9,
  21. 'package_body_offset' => 13
  22. ));
  23. if (!$client->connect($host, $port, 0.5)) {
  24. return false;
  25. }
  26. $client->send($pack);
  27. $pack = @$client->recv();
  28. $client->close();
  29. if ($pack === false) {
  30. //echo $client->errCode . "\n";
  31. //TODO 获取错误码并进行处理
  32. return false;
  33. } else if (empty($pack)) {
  34. echo "close by peer\n";
  35. return false;
  36. }
  37. $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack);
  38. return $ret['data'];
  39. }
  40. $c = 0;
  41. while(1){
  42. $c++;
  43. $ret = remoteCall("127.0.0.1", "5188", '{"controller":"site","methor":"test","params":{"id":'.$c.',"status":0}}');
  44. echo "{$ret}\n";
  45. break;
  46. }
  47. //var_dump($ret);
  48. ?>