设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php完成socket推送技术的示例

发布时间:2022-02-24 13:38 所属栏目:121 来源:互联网
导读:在socket出现之前已经有ajax定时请求、长轮询等方案,但都不能满足需求,socket就应用而生了。 socket基本函数socket 总结下常用的socket函数 服务端:socket_create 创建socket设置基本参数 socket_bind 绑定ip和端口号 socket_listen 监听 socket_accept
  在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,开始贴代码~
 
  --------------分割线
 
  服务端代码:
 
  <?php
  class WS {
   var $master;
   var $sockets = array();
   var $debug = false;//true为调试模式,输出log日志
   var $handshake = array();
   function __construct($address, $port){
   $this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");
   socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");
   socket_bind($this->master, $address, $port)  or die("socket_bind() failed");
   socket_listen($this->master,20)  or die("socket_listen() failed");
     
   $this->sockets[] = $this->master;
   $this->say("Server Started : ".date('Y-m-d H:i:s'));
   $this->say("Listening on : ".$address." port ".$port);
   $this->say("Master socket : ".$this->master."/n");
     
   while(true){
   $socketArr = $this->sockets;
   $write = NULL;
   $except = NULL;
   socket_select($socketArr, $write, $except, NULL); //自动选择来消息的socket 如果是握手 自动选择主机
   foreach ($socketArr as $socket){
   if ($socket == $this->master){ //主机
    $client = socket_accept($this->master);
    if ($client < 0){
    $this->log("socket_accept() failed");
    continue;
    } else{
    $this->connect($client);
    }
   } else {
    $bytes = @socket_recv($socket,$buffer,2048,0);
    if ($bytes == 0){
    $this->disConnect($socket);
    }
    else{
    $key = array_search($socket, $this->sockets);
    if (emptyempty($this->handshake) || !isset($this->handshake[$key]) || !$this->handshake[$key]){
    $this->doHandShake($socket, $buffer, $key);
    }
    else{
    $buffer = $this->decode($buffer);
    echo $buffer.PHP_EOL;
    $key = array_search($socket, $this->sockets);
    $arr = $this->sockets;
    array_shift($arr);
    foreach ($arr as $s){
    $this->send($s, $buffer);
    }
    }
    }
   }
   }
   }
   }
     
     
  new WS('localhost', 4000);
  客户端代码(H5):
 
  <html>
   <head>
   <title>demo</title>
   <script src="https://cdn.boot<a href="http://www.111cn.net/cssdiv/css.html" class="anchor" target="_blank">css</a>.com/jquery/1.9.1/jquery.min.js"></script>
   </head>
   <body>
   <input type="text" id="content">
   <input type="button" value="send" id="send">
   <script type="text/javascript">
    var ws = new WebSocket("ws://localhost:4000");
    ws.onopen = function(){
    console.log("握手成功");
    }
    ws.onmessage = function(e){
    console.log("message:" + e.data);
    }
    ws.onerror = function(){
    console.log("error");
    }
    $("#send").click(function(){
    content = $("#content").val();
    console.log(content);
    ws.send(content);
    })
   </script>
   </body>
  </html>
  然后执行php demo.php 开启socket(从运维那偷学一招,linux下执行nohup php demo.php &可以在后台执行),浏览器打开多个index.html,就能建立通讯了。
 
  代码解析:
 
  1.属性$sockets数组保存每个accept连接(不知道这么描述对不对);
 
  2.属性$handshake数组保存连接是否在连接状态。

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读