脚本宝典收集整理的这篇文章主要介绍了PHP实现的消息实时推送功能【基于反ajax推送】,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了PHP实现的消息实时推送功能。分享给大家供大家参考,具体如下:
入口文件index.htML
ajax处理输入 write.php
<?php /** * Created by TXM. * Time: 2015/4/18 13:13 * function: */ $filename = dirname(__FILE__).'/data.txt'; $isread_file = dirname(__FILE__).'/isread.txt'; $user = dirname(__FILE__).'/user.txt'; //写入消息,消息未读,谁发送的消息 file_put_contents($filename,$_GET['msg']); file_put_contents($isread_file,'0'); file_put_contents($user,$_GET['user']); echo json_encode(array('sf'=>true));
长轮询推送 ajaxPush.php
<?php /** * Created by TXM. * Time: 2015/4/18 13:12 * function: */ $filename = dirname(__FILE__).'/data.txt'; $isread_file = dirname(__FILE__).'/isread.txt'; $userfile = dirname(__FILE__).'/user.txt'; $get_user = $_GET['user'] == '1'?'2':'1'; $msg=''; while(1){ $msg = file_get_contents($filename); $isread = file_get_contents($isread_file); $user = file_get_contents($userfile); //是对方发送的消息,设置消息已读,退出循环。 if($isread == '0' && $get_user == $user){ file_put_contents($isread_file,'1'); break; } sleep(1); } echo json_encode(array('msg'=>$msg));
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+ajax技巧与应用小结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
以上是脚本宝典为你收集整理的PHP实现的消息实时推送功能【基于反ajax推送】全部内容,希望文章能够帮你解决PHP实现的消息实时推送功能【基于反ajax推送】所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。