Home > php教程 > php手册 > 服务器推送数据到客户端(可做直播)

服务器推送数据到客户端(可做直播)

WBOY
Release: 2016-06-07 11:42:51
Original
991 people have browsed it

服务器推送数据到客户端,可实现直播或者一些其他特殊需求。。。(类似每次苹果大会还有爱范儿直播那种东西。如果拿ajax 去定时查询的太累了,实现原理comet 技术)甚至可以执行js css 等代码~
服务器推送数据到客户端(可做直播)
后台前端部分代码~    function send(msg){<br>         $.ajax({<br>             data : {'msg' : msg},<br>             type : 'post',<br>             url : '{:U('Live/SendMsg')}',<br>             success : function(response){<br>                //alert(response);;<br>             }<br>         })<br>     }<br>     $(document).ready(function(){<br>         connect();<br>         $("#btn").click(function(){<br>             var msg = $('#msg').val();<br>             send(msg);<br>             msg.html('');<br>           });<br>     })    public function SendMsg(){<br>         <br>         $filename  = './Uploads/live/'.'data.json';<br>         if ($_POST['msg']!='') {<br>             file_put_contents($filename,$_POST['msg']);<br>             $this->ajaxReturn($_POST,'OK',100);<br>             die();<br>         }else{<br>             $this->ajaxReturn($_POST,'on',0);<br>             die();<br>         }<br>         <br>     }前台展示部分: <div> <br> 1.请输入推送信息,可同时执行多条信息和JavaScript指令,每行一条<br> </div> var timestamp = 0;<br>     var url = '/live.php';<br>     var error = false;<br>     function connect(){<br>         $.ajax({<br>             data : {<br>                 'timestamp' : timestamp<br>             },<br>             url : url,<br>             type : 'get',<br>             timeout : 0,<br>             success : function(response){<br>                 var data = eval('('+response+')');<br>                 error = false;<br>                 timestamp = data.timestamp;<br>                 if (data.msg!='') <br>                 {<br>                     $("#infobox").append(data.msg + '<br>');<br>                 };<br>                 <br>             },<br>             error : function(){<br>                 error = true;<br>                 setTimeout(function(){ connect();}, 5000);<br>             },<br>             complete : function(){<br>                 if (error)<br>                     // if a connection problem occurs, try to reconnect each 5 seconds<br>                     setTimeout(function(){connect();}, 5000);<br>                 else<br>                     connect();<br>             }<br>         })<br>     }<br>    $(document).ready(function(){<br>       connect();<br>     })推送模块代码:<?php <br />     $filename  = './Uploads/live/'.'data.json';<br> //    $msg = isset($_GET['msg']) ? $_GET['msg'] : '';<br>     // 不停的循环,直到储存消息的文件被修改<br>     $lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;<br>     $currentmodif = filemtime($filename);<br>     while ($currentmodif          usleep(100000); // 100ms暂停 缓解CPU压力<br>         clearstatcache(); //清除缓存信息<br>         $currentmodif = filemtime($filename);<br>     }<br>     // 返回json数组<br>     $response = array();<br>     $response['msg']       = file_get_contents($filename);<br>     $response['timestamp'] = $currentmodif;<br>     echo json_encode($response);<br>     //$this->ajaxReturn($response,'ok',1);<br>     flush();<br> ?>服务器推送数据到客户端(可做直播)

现在有个bug 好像是属于tp 内核的吧?在前端页面执行过推送,在去点tp 框架里面其他功能会变得很卡,大概卡顿30秒左右不知道是什么原因还在找,目前采用的是文件读写判断修改时间 然后去推送。也就是说你那个文件只要修改就会去推送到本地客户端,里面可以执行js html css 等你想要的效果。
期待大神优化~

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template