入口文件index.html

<!DOCTYPE HTML>
<html>
<head>
<title>反ajax推送</title>
<style>
.send{color:#555;text-align: left;}
.require{color:blue;text-align: right;}
.content_box{text-align: center;margin: 20px;
border: 1px solid #ddd;padding: 20px;}
</style>
<script src="http://code.jQuery.com/jquery-1.11.2.min.js"></script>
</head>
<body>
<div class="content_box" id="content_box_title" style="border: none;">消息框</div>
<div class="content_box" id="content_box">
</div>
<div style="width: 450px;margin: 0 auto;">
<select id="username" style="font-size: 20px;">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
</select>
<input type="text" style="font-size: 20px;" value="" id="send_text">
<button id="btn_send" style="font-size: 20px;">发送</button>
<button id="btn_link" style="font-size: 20px">连接</button>
</div>
<div class="error_tip" id="error_tip" style="color: red;">
</div>
<script>
$(function(){
//发送消息
$('#btn_send').click(function(){
var send_text = $('#send_text').val();
if(send_text.length <= 0){
$('#error_tip').html('不能输入空值');
}else{
send(send_text);
}
});
//按回车键发送消息
$('#send_text').on('keyup',function(e){
if(e.keyCode == 13){
$('#btn_send').trigger('click');
}
});
//建立通讯链接
$('#btn_link').click(function(){
connect();
var _this = $(this);
_this.attr('disabled',true);
_this.html('已连接');
}); });
//建立通讯连接函数
function connect(){
$('#content_box_title').html($('#username').val()+'的消息窗口');
$.ajax({
data:{'user':$('#username').val()},
url:'ajaxPush.PHP',
type:'get',
timeout:0,
dataType:'json',
success:function(data){
$('#content_box').append('<div class="require">'+data.msg+'</div>');
connect();
}
});
}
//发送消息函数
function send(massege){
var user =$('#username').val();
$.getJSON('write.php',{'msg':massege,'user':user},function(data){
if(data.sf){
$('#content_box').append('<div class="send">'+massege+'</div>');
$('#send_text').val('');
}else{
$('#error_tip').html('输入保存错误!');
}
});
}
</script>
</body>
</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 消息实时推送(反ajax推送)的更多相关文章

  1. 基于HTTP协议之WEB消息实时推送技术原理及实现

    很早就想写一些关于网页消息实时推送技术方面的文章,但是由于最近实在忙,没有时间去写文章.本文主要讲解基于 HTTP1.1 协议的 WEB 推送的技术原理及实现.本人曾经在工作的时候也有做过一些用到网页 ...

  2. 实时显示数据 SignalR 及时消息提醒( 立即向其推送内容)

    实时显示数据  SignalR 及时消息提醒( 立即向其推送内容) http://www.cnblogs.com/Leo_wl/p/5634910.html  <!--Reference the ...

  3. IOS 推送-客户端处理推送消息

    IOS 推送-客户端处理推送消息 1.推送调用顺序 APN push的消息到达后,UIApplicationDelegate有两个方法和处理消息有关: 1)application:didReceive ...

  4. Android 高仿微信实时聊天 基于百度云推送

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38799363 ,本文出自:[张鸿洋的博客] 一直在仿微信界面,今天终于有幸利用百 ...

  5. Qt通过极光推送向app推送消息

    简介 最近在做个项目,当客服端收到防盗的消息通知时向手机app推送一个消息,告知有防盗报警.这么小的功能没必要自己写个推送端,极光推送免费而且推送的成功率高,已经能满足我们的需求了. 极光推送的文档大 ...

  6. iOS本地推送与远程推送

    原文在此 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...

  7. IOS之推送通知(本地推送和远程推送)

    推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...

  8. ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送

    iOS10 已经 "deprected" 我们的UILocalNotification 采用了全新的UNUserNotificationCenter; 1 首先,你需要引进< ...

  9. android推送-PHP(第三方推送:个推)

    在项目初期,就安卓推送功能怎么做,曾经参考过例如XMPP之类的推送方法.但苦于那些是些英文档案,又没太多时间研究,所以打算采用第三方推送(个推,极光推送等),后来在美图技术老大推荐下用采用个推. PS ...

随机推荐

  1. iframe自适应高度(兼容IE 火狐 谷歌)

    <div id="leamain"> <iframe src="#" marginheight="0" marginwid ...

  2. 转:Oracle表分区

    Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: 1. create table graderecord 2. ...

  3. Spark SQL External Data Sources JDBC官方实现读测试

    在最新的master分支上官方提供了Spark JDBC外部数据源的实现,先尝为快. 通过spark-shell测试: import org.apache.spark.sql.SQLContext v ...

  4. 使用gson解析,生成Json

    包:gson-2.3.jarJson文本解析为Java对象:Java对象生成为Json文本 import com.google.gson.Gson; public class TestGson { c ...

  5. tomacat启动慢

    tomcat目录下有多个工程,tomcat每次启动的时候都会去加载,删除不需要的工程,

  6. PDF 生成插件 flying saucer 和 iText

    最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...

  7. pt-query-digest查询日志分析工具

    1.工具介绍 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcp ...

  8. session基础

    1.每个页面都必须开启session_start()后才能在每个页面里面使用session. 2.session_start()初始化session,第一次访问会生成一个唯一会话ID保存在客户端(是基 ...

  9. C语言基础_2

    scanf函数可以从键盘上读取数据并记录到变量中.为了使用这个函数也需要在文件开头使用如下的预处理指令#include <stdio.h>scanf函数使用的时候所需要的初始数据和prin ...

  10. Shape comparison language

      形状比较语言, 九交模型 In this topic About shape comparison language Dimensionality Extensions to the CBM SC ...