qserverSocket.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Created by xwx
  4. * Date: 2017/10/18
  5. * Time: 14:33
  6. */
  7. class SocketService
  8. {
  9. private $address = '0.0.0.0';
  10. private $port = 8083;
  11. private $_sockets;
  12. public function __construct($address = '', $port='')
  13. {
  14. if(!empty($address)){
  15. $this->address = $address;
  16. }
  17. if(!empty($port)) {
  18. $this->port = $port;
  19. }
  20. }
  21. public function service(){
  22. //获取tcp协议号码。
  23. $tcp = getprotobyname("tcp");
  24. $sock = socket_create(AF_INET, SOCK_STREAM, $tcp);
  25. socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
  26. if($sock < 0)
  27. {
  28. throw new Exception("failed to create socket: ".socket_strerror($sock)."\n");
  29. }
  30. socket_bind($sock, $this->address, $this->port);
  31. socket_listen($sock, $this->port);
  32. echo "listen on $this->address $this->port ... \n";
  33. $this->_sockets = $sock;
  34. }
  35. public function run(){
  36. $this->service();
  37. $clients[] = $this->_sockets;
  38. while (true){
  39. $changes = $clients;
  40. $write = NULL;
  41. $except = NULL;
  42. socket_select($changes, $write, $except, NULL);
  43. foreach ($changes as $key => $_sock){
  44. if($this->_sockets == $_sock){ //判断是不是新接入的socket
  45. if(($newClient = socket_accept($_sock)) === false){
  46. die('failed to accept socket: '.socket_strerror($_sock)."\n");
  47. }
  48. $line = trim(socket_read($newClient, 1024));
  49. $this->handshaking($newClient, $line);
  50. //获取client ip
  51. socket_getpeername ($newClient, $ip);
  52. $clients[$ip] = $newClient;
  53. echo "Client ip:{$ip} \n";
  54. echo "Client msg:{$line} \n";
  55. } else {
  56. socket_recv($_sock, $buffer, 2048, 0);
  57. $msg = $this->message($buffer);
  58. //在这里业务代码
  59. echo "{$key} clinet msg:",$msg,"\n";
  60. fwrite(STDOUT, 'Please input a argument:');
  61. $response = trim(fgets(STDIN));
  62. $this->send($_sock, $response);
  63. echo "{$key} response to Client:".$response,"\n";
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * 握手处理
  70. * @param $newClient socket
  71. * @return int 接收到的信息
  72. */
  73. public function handshaking($newClient, $line){
  74. $headers = array();
  75. $lines = preg_split("/\r\n/", $line);
  76. foreach($lines as $line)
  77. {
  78. $line = chop($line);
  79. if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
  80. {
  81. $headers[$matches[1]] = $matches[2];
  82. }
  83. }
  84. $secKey = $headers['Sec-WebSocket-Key'];
  85. $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
  86. $upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
  87. "Upgrade: websocket\r\n" .
  88. "Connection: Upgrade\r\n" .
  89. "WebSocket-Origin: $this->address\r\n" .
  90. "WebSocket-Location: ws://$this->address:$this->port/websocket/websocket\r\n".
  91. "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
  92. return socket_write($newClient, $upgrade, strlen($upgrade));
  93. }
  94. /**
  95. * 解析接收数据
  96. * @param $buffer
  97. * @return null|string
  98. */
  99. public function message($buffer){
  100. $len = $masks = $data = $decoded = null;
  101. $len = ord($buffer[1]) & 127;
  102. if ($len === 126) {
  103. $masks = substr($buffer, 4, 4);
  104. $data = substr($buffer, 8);
  105. } else if ($len === 127) {
  106. $masks = substr($buffer, 10, 4);
  107. $data = substr($buffer, 14);
  108. } else {
  109. $masks = substr($buffer, 2, 4);
  110. $data = substr($buffer, 6);
  111. }
  112. for ($index = 0; $index < strlen($data); $index++) {
  113. $decoded .= $data[$index] ^ $masks[$index % 4];
  114. }
  115. return $decoded;
  116. }
  117. /**
  118. * 发送数据
  119. * @param $newClinet 新接入的socket
  120. * @param $msg 要发送的数据
  121. * @return int|string
  122. */
  123. public function send($newClinet, $msg){
  124. $msg = $this->frame($msg);
  125. socket_write($newClinet, $msg, strlen($msg));
  126. }
  127. public function frame($s) {
  128. $a = str_split($s, 125);
  129. if (count($a) == 1) {
  130. return "\x81" . chr(strlen($a[0])) . $a[0];
  131. }
  132. $ns = "";
  133. foreach ($a as $o) {
  134. $ns .= "\x81" . chr(strlen($o)) . $o;
  135. }
  136. return $ns;
  137. }
  138. /**
  139. * 关闭socket
  140. */
  141. public function close(){
  142. return socket_close($this->_sockets);
  143. }
  144. }
  145. $sock = new SocketService();
  146. $sock->run();