Socket.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Websocket;
  36. use Hoa\Socket as HoaSocket;
  37. /**
  38. * Class \Hoa\Websocket\Socket.
  39. *
  40. * WebSocket specific socket and transports.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class Socket extends HoaSocket
  46. {
  47. /**
  48. * Endpoint.
  49. *
  50. * @var string
  51. */
  52. protected $_endPoint = '/';
  53. /**
  54. * Constructor
  55. *
  56. * @param string $uri Socket URI.
  57. * @param boolean $secured Whether the connection is secured.
  58. * @param string $endPoint Endpoint.
  59. */
  60. public function __construct($uri, $secured = false, $endPoint = '/')
  61. {
  62. parent::__construct($uri);
  63. $this->_secured = $secured;
  64. $this->_endPoint = $endPoint;
  65. return;
  66. }
  67. /**
  68. * Retrieve the websocket endpoint
  69. *
  70. * @return string
  71. */
  72. public function getEndPoint()
  73. {
  74. return $this->_endPoint;
  75. }
  76. /**
  77. * Factory to create a valid `Hoa\Socket\Socket` object.
  78. *
  79. * @param string $socketUri URI of the socket to connect to.
  80. * @return void
  81. */
  82. public static function transportFactory($socketUri)
  83. {
  84. $parsed = parse_url($socketUri);
  85. if (false === $parsed || !isset($parsed['host'])) {
  86. throw new Exception(
  87. 'URI %s seems invalid, cannot parse it.',
  88. 0,
  89. $socketUri
  90. );
  91. }
  92. $secure =
  93. isset($parsed['scheme'])
  94. ? 'wss' === $parsed['scheme']
  95. : false;
  96. $port =
  97. isset($parsed['port'])
  98. ? $parsed['port']
  99. : (true === $secure
  100. ? 443
  101. : 80);
  102. return new static(
  103. 'tcp://' . $parsed['host'] . ':' . $port,
  104. $secure,
  105. (isset($parsed['path']) ? $parsed['path'] : '/') .
  106. (isset($parsed['query']) ? '?' . $parsed['query'] : '')
  107. );
  108. }
  109. }
  110. /**
  111. * Register `ws://` and `wss://` transports.
  112. */
  113. HoaSocket\Transport::register('ws', ['Hoa\Websocket\Socket', 'transportFactory']);
  114. HoaSocket\Transport::register('wss', ['Hoa\Websocket\Socket', 'transportFactory']);