php socket 和 html5 websocket 通讯

79 篇文章 1 订阅

window下  运行方式


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


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



PHP 服务器 代码  


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



HTML5 客户端代码


[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <meta charset="UTF-8">  
  6.     <title>websocket</title>  
  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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值