扣丁学堂PHP培训之解析用php写简单消息推送(源码)

2018-02-05 13:53:09 647浏览

入口代码index.html

<!DOCTYPEHTML>

<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:1pxsolid#ddd;padding:20px;}

</style>

<scriptsrc="http://www.codingke.com/jquery-1.11.2.min.js"></script>

</head>

<body>

<divclass="content_box"id="content_box_title"style="border:none;">消息框</div>

<divclass="content_box"id="content_box">

</div>

<divstyle="width:450px;margin:0auto;">

<selectid="username"style="font-size:20px;">

<optionvalue="1"selected="selected">1</option>

<optionvalue="2">2</option>

</select>

<inputtype="text"style="font-size:20px;"value=""id="send_text">

<buttonid="btn_send"style="font-size:20px;">发送</button>

<buttonid="btn_link"style="font-size:20px">连接</button>

</div>

<divclass="error_tip"id="error_tip"style="color:red;">

</div>

<script>

$(function(){

//发送消息

$('#btn_send').click(function(){

varsend_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('已连接');

});

});

//建立通讯连接函数

functionconnect(){

$('#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('<divclass="require">'+data.msg+'</div>');

connect();

}

});

}

//发送消息函数

functionsend(massege){

varuser=$('#username').val();

$.getJSON('write.php',{'msg':massege,'user':user},function(data){

if(data.sf){

$('#content_box').append('<divclass="send">'+massege+'</div>');

$('#send_text').val('');

}else{

$('#error_tip').html('输入保存错误!');

}

});

}

</script>

</body>

</html>

ajax处理输入write.php

<?php

/**

*CreatedbyTXM.

*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']);

echojson_encode(array('sf'=>true));

长轮询推送ajaxPush.php

<?php

/**

*CreatedbyTXM.

*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);

}

echojson_encode(array('msg'=>$msg));

以上就是入口代码index.html

<!DOCTYPEHTML>

<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:1pxsolid#ddd;padding:20px;}

</style>

<scriptsrc="http://www.codingke.com/jquery-1.11.2.min.js"></script>

</head>

<body>

<divclass="content_box"id="content_box_title"style="border:none;">消息框</div>

<divclass="content_box"id="content_box">

</div>

<divstyle="width:450px;margin:0auto;">

<selectid="username"style="font-size:20px;">

<optionvalue="1"selected="selected">1</option>

<optionvalue="2">2</option>

</select>

<inputtype="text"style="font-size:20px;"value=""id="send_text">

<buttonid="btn_send"style="font-size:20px;">发送</button>

<buttonid="btn_link"style="font-size:20px">连接</button>

</div>

<divclass="error_tip"id="error_tip"style="color:red;">

</div>

<script>

$(function(){

//发送消息

$('#btn_send').click(function(){

varsend_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('已连接');

});

});

//建立通讯连接函数

functionconnect(){

$('#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('<divclass="require">'+data.msg+'</div>');

connect();

}

});

}

//发送消息函数

functionsend(massege){

varuser=$('#username').val();

$.getJSON('write.php',{'msg':massege,'user':user},function(data){

if(data.sf){

$('#content_box').append('<divclass="send">'+massege+'</div>');

$('#send_text').val('');

}else{

$('#error_tip').html('输入保存错误!');

}

});

}

</script>

</body>

</html>

ajax处理输入write.php

<?php

/**

*CreatedbyTXM.

*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']);

echojson_encode(array('sf'=>true));

长轮询推送ajaxPush.php

<?php

/**

*CreatedbyTXM.

*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);

}

echojson_encode(array('msg'=>$msg));

以上就是关于扣丁学堂解析用php写简单消息推送(源码)的详细介绍,最后想要了解更多关于PHP开发发展前景趋势,请关注扣丁学堂官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的PHP培训视频教程系统,通过千锋扣丁学堂金牌讲师在线录制的一套PHP视频教程课程,让你快速掌握PHP从入门到精通开发实战技能。扣丁学堂PHP开发工程师技术交流群:374332265。




扣丁学堂微信公众号



关注微信公众号获取更多学习资料



查看更多关于“php培训资讯的相关文章>>


标签: PHP培训 PHP视频教程 PHP从入门到精通 PHP学习路线图 PHP开发工程师

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号