zoukankan      html  css  js  c++  java
  • php实现socket推送技术

    在socket出现之前已经有ajax定时请求、长轮询等方案,但都不能满足需求,socket就应用而生了。

    socket基本函数socket

    总结下常用的socket函数

    服务端: socket_create 创建socket设置基本参数 

         socket_bind 绑定ip和端口号   

         socket_listen 监听

         socket_accept 客户端的连接

         socket_read 读取客户端的数据

         socket_write 给单独客户端发送数据 

         socket_close 关闭连接

    客户端:socket_create 创建socket设置基本参数 

         socket_connect 连接socket

         socket_write 给服务端发送数据

         socket_read 读取服务端数据

         socket_close 关闭连接

    H5websocket不多说了,上链接

    OK,开始贴代码~

    ----------------------------------------------------------分割线

    服务端代码:

      1 <?php
      2 class WS {
      3     var $master;
      4     var $sockets = array();
      5     var $debug = false;//true为调试模式,输出log日志
      6     var $handshake = array();
      7 
      8     function __construct($address, $port){
      9         $this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed");
     10         socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1)  or die("socket_option() failed");
     11         socket_bind($this->master, $address, $port)                    or die("socket_bind() failed");
     12         socket_listen($this->master,20)                                or die("socket_listen() failed");
     13         
     14         $this->sockets[] = $this->master;
     15         $this->say("Server Started : ".date('Y-m-d H:i:s'));
     16         $this->say("Listening on   : ".$address." port ".$port);
     17         $this->say("Master socket  : ".$this->master."\n");
     18         
     19         while(true){
     20             $socketArr = $this->sockets;
     21             $write = NULL;
     22             $except = NULL;
     23             socket_select($socketArr, $write, $except, NULL);  //自动选择来消息的socket 如果是握手 自动选择主机
     24             foreach ($socketArr as $socket){
     25                 if ($socket == $this->master){  //主机
     26                     $client = socket_accept($this->master);
     27                     if ($client < 0){
     28                         $this->log("socket_accept() failed");
     29                         continue;
     30                     } else{
     31                         $this->connect($client);
     32                     }
     33                 } else {
     34                     $bytes = @socket_recv($socket,$buffer,2048,0);
     35                     if ($bytes == 0){
     36                         $this->disConnect($socket);
     37                     }
     38                     else{
     39                         $key = array_search($socket, $this->sockets);
     40                         if (empty($this->handshake) || !isset($this->handshake[$key]) || !$this->handshake[$key]){
     41                             $this->doHandShake($socket, $buffer, $key);
     42                         }
     43                         else{
     44                             $buffer = $this->decode($buffer);
     45                             echo $buffer.PHP_EOL;
     46                             $key = array_search($socket, $this->sockets);
     47                             $arr = $this->sockets;
     48                             array_shift($arr);
     49                             foreach ($arr as $s){
     50                                 $this->send($s, $buffer);
     51                             }
     52                         }
     53                     }
     54                 }
     55             }
     56         }
     57     }
     58     
     59     function send($client, $msg){
     60         $msg = $this->frame($msg);
     61         socket_write($client, $msg, strlen($msg));
     62     }
     63     function connect($socket){
     64         array_push($this->sockets, $socket);
     65         $this->say("\n" . $socket . " CONNECTED!");
     66         $this->say(date("Y-n-d H:i:s"));
     67     }
     68     function disConnect($socket){
     69         $index = array_search($socket, $this->sockets);
     70         socket_close($socket);
     71         $this->say($socket . " DISCONNECTED!");
     72         if ($index >= 0){
     73             echo 'unset index is:'.PHP_EOL;
     74             unset($this->sockets[$index]);
     75         }
     76     }
     77     function doHandShake($socket, $buffer, $handKey){
     78         $this->log("\nRequesting handshake...");
     79         $this->log($buffer);
     80         list($resource, $host, $origin, $key) = $this->getHeaders($buffer);
     81         $this->log("Handshaking...");
     82         $upgrade  = "HTTP/1.1 101 Switching Protocol\r\n" .
     83                     "Upgrade: websocket\r\n" .
     84                     "Connection: Upgrade\r\n" .
     85                     "Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n\r\n";  //必须以两个回车结尾
     86         $this->log($upgrade);
     87         $sent = socket_write($socket, $upgrade, strlen($upgrade));
     88         $this->handshake[$handKey]=true;
     89         $this->log("Done handshaking...");
     90         return true;
     91     }
     92 
     93     function getHeaders($req){
     94         $r = $h = $o = $key = null;
     95         if (preg_match("/GET (.*) HTTP/"              ,$req,$match)) { $r = $match[1]; }
     96         if (preg_match("/Host: (.*)\r\n/"             ,$req,$match)) { $h = $match[1]; }
     97         if (preg_match("/Origin: (.*)\r\n/"           ,$req,$match)) { $o = $match[1]; }
     98         if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)) { $key = $match[1]; }
     99         return array($r, $h, $o, $key);
    100     }
    101 
    102     function calcKey($key){
    103         //基于websocket version 13
    104         $accept = base64_encode(sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
    105         return $accept;
    106     }
    107 
    108     function decode($buffer) {
    109         $len = $masks = $data = $decoded = null;
    110         $len = ord($buffer[1]) & 127;
    111 
    112         if ($len === 126) {
    113             $masks = substr($buffer, 4, 4);
    114             $data = substr($buffer, 8);
    115         } 
    116         else if ($len === 127) {
    117             $masks = substr($buffer, 10, 4);
    118             $data = substr($buffer, 14);
    119         } 
    120         else {
    121             $masks = substr($buffer, 2, 4);
    122             $data = substr($buffer, 6);
    123         }
    124         for ($index = 0; $index < strlen($data); $index++) {
    125             $decoded .= $data[$index] ^ $masks[$index % 4];
    126         }
    127         return $decoded;
    128     }
    129 
    130     function frame($s){
    131         $a = str_split($s, 125);
    132         if (count($a) == 1){
    133             return "\x81" . chr(strlen($a[0])) . $a[0];
    134         }
    135         $ns = "";
    136         foreach ($a as $o){
    137             $ns .= "\x81" . chr(strlen($o)) . $o;
    138         }
    139         return $ns;
    140     }
    141 
    142     
    143     function say($msg = ""){
    144         echo $msg . "\n";
    145     }
    146     function log($msg = ""){
    147         if ($this->debug){
    148             echo $msg . "\n";
    149         } 
    150     }
    151 }
    152     
    153 
    154 new WS('localhost', 4000);

    客户端代码(H5):

     1 <html>
     2     <head>
     3         <title>demo</title>
     4         <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
     5     </head>
     6     <body>
     7     <input type="text" id="content">
     8     <input type="button" value="send" id="send">
     9         <script type="text/javascript">
    10             var ws = new WebSocket("ws://localhost:4000");
    11             ws.onopen = function(){
    12                 console.log("握手成功");
    13             }
    14             ws.onmessage = function(e){
    15                 console.log("message:" + e.data);
    16             }
    17             ws.onerror = function(){
    18                 console.log("error");
    19             }
    20             $("#send").click(function(){
    21                 content = $("#content").val();
    22                 console.log(content);
    23                 ws.send(content);
    24             })
    25         </script>
    26     </body>
    27 </html>

    然后执行php demo.php 开启socket(从运维那偷学一招,linux下执行nohup php demo.php &可以在后台执行),浏览器打开多个index.html,就能建立通讯了。

    代码解析:

    1.属性$sockets数组保存每个accept连接(不知道这么描述对不对); 

    2.属性$handshake数组保存连接是否在连接状态;

  • 相关阅读:
    join_tab计算代价
    outer join test
    突然觉得mysql优化器蛮简单
    将数据库字段从float修改为decimal
    小米初体验
    简述安装android开发环境
    Rust语言:安全地并发
    awk里的各种坑
    ubuntu下使用C语言开发一个cgi程序
    Ubuntu下安装和配置Apache2
  • 原文地址:https://www.cnblogs.com/kkform/p/8072836.html
Copyright © 2011-2022 走看看