echoserver.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. error_reporting(-1);
  3. /**
  4. * This file is used for the tests, but can also serve as an example of a WebSocket\Server.
  5. */
  6. require(dirname(dirname(__FILE__)) . '/vendor/autoload.php');
  7. use WebSocket\Server;
  8. // Setting timeout to 200 seconds to make time for all tests and manual runs.
  9. $server = new Server(array('timeout' => 200));
  10. echo $server->getPort(), "\n";
  11. while ($server->accept()) {
  12. try {
  13. while (true) {
  14. $message = $server->receive();
  15. echo "Received $message\n\n";
  16. if ($message === 'exit') {
  17. echo microtime(true), " Client told me to quit. Bye bye.\n";
  18. echo microtime(true), " Close response: ", $server->close(), "\n";
  19. echo microtime(true), " Close status: ", $server->getCloseStatus(), "\n";
  20. exit;
  21. }
  22. if ($message === 'Dump headers') {
  23. $server->send(implode("\r\n", $server->getRequest()));
  24. }
  25. if ($message === 'ping') {
  26. $server->send('ping', 'ping', true);
  27. }
  28. elseif ($auth = $server->getHeader('Authorization')) {
  29. $server->send("$auth - $message", 'text', false);
  30. }
  31. else {
  32. $server->send($message, 'text', false);
  33. }
  34. }
  35. }
  36. catch (WebSocket\ConnectionException $e) {
  37. echo "\n", microtime(true), " Connection died: $e\n";
  38. }
  39. }