Transport.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace PHPSocketIO\Engine;
  3. use \PHPSocketIO\Event\Emitter;
  4. use \PHPSocketIO\Debug;
  5. class Transport extends Emitter
  6. {
  7. public $readyState = 'opening';
  8. public $req = null;
  9. public $res = null;
  10. public function __construct()
  11. {
  12. Debug::debug('Transport __construct no access !!!!');
  13. }
  14. public function __destruct()
  15. {
  16. Debug::debug('Transport __destruct');
  17. }
  18. public function noop()
  19. {
  20. }
  21. public function onRequest($req)
  22. {
  23. $this->req = $req;
  24. }
  25. public function close($fn = null)
  26. {
  27. $this->readyState = 'closing';
  28. $fn = $fn ? $fn : array($this, 'noop');
  29. $this->doClose($fn);
  30. }
  31. public function onError($msg, $desc = '')
  32. {
  33. if ($this->listeners('error'))
  34. {
  35. $err = array(
  36. 'type' => 'TransportError',
  37. 'description' => $desc,
  38. );
  39. $this->emit('error', $err);
  40. }
  41. else
  42. {
  43. echo("ignored transport error $msg $desc\n");
  44. }
  45. }
  46. public function onPacket($packet)
  47. {
  48. $this->emit('packet', $packet);
  49. }
  50. public function onData($data)
  51. {
  52. $this->onPacket(Parser::decodePacket($data));
  53. }
  54. public function onClose()
  55. {
  56. $this->req = $this->res = null;
  57. $this->readyState = 'closed';
  58. $this->emit('close');
  59. $this->removeAllListeners();
  60. }
  61. public function destroy()
  62. {
  63. $this->req = $this->res = null;
  64. $this->readyState = 'closed';
  65. $this->removeAllListeners();
  66. $this->shouldClose = null;
  67. }
  68. }