UdpConnectionTest.php 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. //example from manual
  3. use Workerman\Connection\AsyncUdpConnection;
  4. use Workerman\Timer;
  5. use Workerman\Worker;
  6. it('tests udp connection', function () {
  7. /** @noinspection PhpObjectFieldsAreOnlyWrittenInspection */
  8. $server = new Worker('udp://0.0.0.0:9292');
  9. $server->onMessage = function ($connection, $data) {
  10. expect($data)->toBe('hello');
  11. $connection->send('xiami');
  12. };
  13. $server->onWorkerStart = function () {
  14. //client
  15. Timer::add(1, function () {
  16. $client = new AsyncUdpConnection('udp://127.0.0.1:1234');
  17. $client->onConnect = function ($client) {
  18. $client->send('hello');
  19. };
  20. $client->onMessage = function ($client, $data) {
  21. expect($data)->toBe('xiami');
  22. //terminal this test
  23. terminate_current_test();
  24. };
  25. $client->connect();
  26. }, null, false);
  27. };
  28. Worker::runAll();
  29. })->skipOnWindows() //require posix, multiple workers
  30. ->skip(message: 'this test needs to run isolated process while pest not support doing so yet');