php socket 和 html5 websocket 通讯

window下  运行方式


 将以下cmd命令 保存到文本  另存为  cli .bat  文件  ,双击运行。


[plain]  view plain  copy
  1. D:\php7\php.exe   E:\www\Server_socket.php  



PHP 服务器 代码  


[php]  view plain  copy
  1.   
  2. /** 
  3. * 聊天室服务器  websocket 专用 
  4. */  
  5. class Server_socket  
  6. {  
  7.     private $socket;  
  8.     private $accept = [];  
  9.     private $hands = [];  
  10.     function __construct($host$port$max)  
  11.     {  
  12.         $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);  
  13.         socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, TRUE);  
  14.         socket_bind($this->socket, $host,$port);  
  15.         socket_listen($this->socket,$max);   
  16.         print_r($this->socket);  
  17.     }  
  18.   
  19.     public function start()  
  20.     {  
  21.         while (true) {  
  22.   
  23.             $cycle = $this->accept;  
  24.             $cycle[] = $this->socket;  
  25.             socket_select($cycle$write$except, null);  
  26.   
  27.             foreach ($cycle as $sock) {  
  28.                 if ($sock == $this->socket) {  
  29.                     $this->accept[] = socket_accept($sock);  
  30.                     $arr =  array_keys($this->accept);  
  31.                     $key = end($arr);  
  32.                     $this->hands[$key] = false;  
  33.                 }else{  
  34.                     $length = socket_recv($sock$buffer, 204800, null);  
  35.                     $key = array_search($sock$this->accept);  
  36.                     if (!$this->hands[$key]) {  
  37.                         $this->dohandshake($sock,$buffer,$key);  
  38.                     }else if($length < 1){  
  39.                         $this->close($sock);  
  40.                     }else{  
  41.                         // 解码  
  42.                         $data = $this->decode($buffer);  
  43.                         print_r($data);  
  44.                         //编码  
  45.                         $data = $this->encode($data);  
  46.                         print_r($data);  
  47.                         //发送  
  48.                         foreach ($this->accept as $client) {  
  49.                             socket_write($client$data,strlen($data));  
  50.                         }     
  51.                     }          
  52.                 }  
  53.             }  
  54.             sleep(1);  
  55.         }  
  56.     }/* end of start*/  
  57.   
  58.     /** 
  59.      * 首次与客户端握手 
  60.      */  
  61.     public function dohandshake($sock$data$key) {  
  62.         if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/"$data$match)) {  
  63.             $response = base64_encode(sha1($match[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));  
  64.             $upgrade  = "HTTP/1.1 101 Switching Protocol\r\n" .  
  65.                     "Upgrade: websocket\r\n" .  
  66.                     "Connection: Upgrade\r\n" .  
  67.                     "Sec-WebSocket-Accept: " . $response . "\r\n\r\n";  
  68.             socket_write($sock$upgradestrlen($upgrade));  
  69.             $this->hands[$key] = true;  
  70.         }  
  71.     }/*dohandshake*/  
  72.   
  73.     /** 
  74.      * 关闭一个客户端连接 
  75.      */  
  76.     public function close($sock) {  
  77.         $key = array_search($sock$this->accept);  
  78.         socket_close($sock);  
  79.         unset($this->accept[$key]);  
  80.         unset($this->hands[$key]);  
  81.     }  
  82.   
  83.     /** 
  84.      * 字符解码 
  85.      */  
  86.     public function decode($buffer) {  
  87.         $len = $masks = $data = $decoded = null;  
  88.         $len = ord($buffer[1]) & 127;  
  89.         if ($len === 126) {  
  90.             $masks = substr($buffer, 4, 4);  
  91.             $data = substr($buffer, 8);  
  92.         }   
  93.         else if ($len === 127) {  
  94.             $masks = substr($buffer, 10, 4);  
  95.             $data = substr($buffer, 14);  
  96.         }   
  97.         else {  
  98.             $masks = substr($buffer, 2, 4);  
  99.             $data = substr($buffer, 6);  
  100.         }  
  101.         for ($index = 0; $index < strlen($data); $index++) {  
  102.             $decoded .= $data[$index] ^ $masks[$index % 4];  
  103.         }  
  104.         return $decoded;  
  105.     }  
  106.   
  107.     /** 
  108.      * 字符编码 
  109.      */  
  110.     public function encode($buffer) {  
  111.         $length = strlen($buffer);  
  112.         if($length <= 125) {  
  113.             return "\x81".chr($length).$buffer;  
  114.         } else if($length <= 65535) {  
  115.             return "\x81".chr(126).pack("n"$length).$buffer;  
  116.         } else {  
  117.             return "\x81".char(127).pack("xxxxN"$length).$buffer;  
  118.         }  
  119.     }  
  120.   
  121. }/* end of class Server_socket*/  
  122.   
  123. $server_socket = new Server_socket('127.0.0.1',8008,1000);  
  124. $server_socket->start(); sleep(1000); ?>  



HTML5 客户端代码


[html]  view plain  copy
  1. >  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="UTF-8">  
  6.     <title>websockettitle>  
  7. head>  
  8.   
  9. <body>  
  10.     <div>  
  11.         <button id='send'> 发送button>  
  12.     div>  
  13. body>  
  14. <script>  
  15. var socket = new WebSocket('ws://127.0.0.1:8008');  
  16. socket.onopen = function(event) {  
  17.     alert('连接');  
  18. };  
  19.   
  20. socket.onmessage = function(event) {  
  21.     var content = event.data;  
  22.     if (content.length > 2) {  
  23.         alert(content);  
  24.     }  
  25. };  
  26. var send = document.getElementById('send');  
  27. send.addEventListener('click', function() {  
  28.   
  29.     var content = '123456789'  
  30.     socket.send(content);  
  31.     alert('发送');  
  32.   
  33. });  
  34. script>  
  35.   
  36. html>  



转自:http://blog.csdn.net/luochengquan/article/details/62041811



你可能感兴趣的:(web开发,php,php,socket,websocket,html5,通讯)