php 消息实时推送(反ajax推送)
入口文件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推送)的更多相关文章
- 基于HTTP协议之WEB消息实时推送技术原理及实现
很早就想写一些关于网页消息实时推送技术方面的文章,但是由于最近实在忙,没有时间去写文章.本文主要讲解基于 HTTP1.1 协议的 WEB 推送的技术原理及实现.本人曾经在工作的时候也有做过一些用到网页 ...
- 实时显示数据 SignalR 及时消息提醒( 立即向其推送内容)
实时显示数据 SignalR 及时消息提醒( 立即向其推送内容) http://www.cnblogs.com/Leo_wl/p/5634910.html <!--Reference the ...
- IOS 推送-客户端处理推送消息
IOS 推送-客户端处理推送消息 1.推送调用顺序 APN push的消息到达后,UIApplicationDelegate有两个方法和处理消息有关: 1)application:didReceive ...
- Android 高仿微信实时聊天 基于百度云推送
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38799363 ,本文出自:[张鸿洋的博客] 一直在仿微信界面,今天终于有幸利用百 ...
- Qt通过极光推送向app推送消息
简介 最近在做个项目,当客服端收到防盗的消息通知时向手机app推送一个消息,告知有防盗报警.这么小的功能没必要自己写个推送端,极光推送免费而且推送的成功率高,已经能满足我们的需求了. 极光推送的文档大 ...
- iOS本地推送与远程推送
原文在此 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...
- IOS之推送通知(本地推送和远程推送)
推送通知和NSNotification是有区别的: NSNotification:是看不到的 推送通知:是可以看到的 IOS中提供了两种推送通知 本地推送通知:(Local Notification) ...
- ios10 UNNtificationRequest UNUserNotificationCenter的应用 推送之本地推送
iOS10 已经 "deprected" 我们的UILocalNotification 采用了全新的UNUserNotificationCenter; 1 首先,你需要引进< ...
- android推送-PHP(第三方推送:个推)
在项目初期,就安卓推送功能怎么做,曾经参考过例如XMPP之类的推送方法.但苦于那些是些英文档案,又没太多时间研究,所以打算采用第三方推送(个推,极光推送等),后来在美图技术老大推荐下用采用个推. PS ...
随机推荐
- [Tex学习笔记]一个数学公式
\begin{equation*} \begin{aligned} &\quad\int |\nabla(T_1-\overline{T})^+|^2 \rm dx-\int \frac{3m ...
- 46. Partition List
Partition List Given a linked list and a value x, partition it such that all nodes less than x come ...
- tiny学习3
这几天在埋头写自己的个星期!而且由于它是基于事件发生的次序(小时就把我的文件导出来了--呵呵.在阅读本文之前,请先看看我Blog里转贴的<TinyXML学习笔记>,相信它能给各位一个关于T ...
- oracle查看所有表的数据量并排序
源地址:http://blog.csdn.net/zhanggnol/article/details/6683697 select t.table_name,t.num_rows from user_ ...
- C++ 创建和遍历二叉树
一个简单的创建和遍历二叉树的C++程序,二叉树的其他操作程序待更新. #include <iostream> using namespace std; struct BiTNode{ ch ...
- Android--Toast(吐司)的基本使用
1.学习Android开发,不能从单方面的知识去考虑问题,要从全面出发. 2.Toast(吐司) 1.打开eclipse 选择文件存放的位置 点击Dbms,测试是否连接成功(海马模拟器或者夜神模拟器) ...
- css之让文字在一定范围内显示,不超过固定的宽度和高度
- IOS App Integrate Google Map Problems and Method to solve them
1. You must get a key in google developer center, and register it in below function in AppDelegate.m ...
- information_schema系列之字符集校验(CHARACTER_SETS,COLLATIONS,COLLATION_CHARACTER_SET_APPLICABILITY)
1:CHARACTER_SETS 首先看一下查询前十条的结果: root@localhost [information_schema]>select * from CHARACTER_SETS ...
- Linux C学习笔记07--管道通信
管道通信属于进程间通信的一种方式,使用方便,但是局限是父进程与子进程间的通信,下面是调试好的代码. 程序父进程创建2个管道,分别用于父进程写数据--子进程读数据和子进程写数据--父进程读数据: #in ...