一个iframe实现长轮询,通过PHP查询数据库并用JS更新页面内容的程序,问题是并不是每一条MYSQL的INSERT消息都能显示到页面,求帮忙分析下哪里有问题 本帖最后由 stneo1990 于 2013-08-08 11:45:06 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 前台kefu.html代码:<?php setcookie('username','admin');?><!doctype html><html> <head> <title>在线客服系统</title> <meta charset="utf-8" /> <script> //测试浏览器是Chrome 27.0.1453.93 var xhr = new XMLHttpRequest(); //由query.php程序调用的JS函数,用来输出内容到前台页面 function comet(msg,rand){ var content = ''; //获取用户发送给前台的消息,并拼接好 content = '<span onclick="reply(this);" style="cursor:pointer;">' + msg.pos + ' </span>' + '对你说:<br/> ' + msg.content + '<br/>'; var msgzone = document.getElementById('msgzone'); //把拼接的消息赋值给消息显示区域 msgzone.innerHTML += content; } //点击某个用户名,然后回复的span会显示该用户名 function reply(reobj){ document.getElementById('rec').innerHTML = reobj.innerHTML } //前台客服回复时,调用的函数 function huifu(){ var rep = ''; //接收前台发送的用户名字 var rec = document.getElementById('rec').innerHTML; //前台发送的消息内容 var content = document.getElementsByTagName('textarea')[0].value; if(rec == '' || content == ''){ alert('请选择回复人并填写回复内容');//简单判断回复对象和内容不能为空 return; } //sendmsg.php是专门配套前台发送消息用的,就是把消息INSERT进数据库 xhr.open('POST','sendmsg.php',true); xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); xhr.onreadystatechange = function(){ if(this.readyState == 4 && this.status == 200){ if(this.responseText == 'ok'){ //当确定前台发送的消息INSERT进数据库后,把这条消息显示到消息区域内 rep += '你对 ' + rec + '说:<br/> ' + content + '<br/>'; var msgzone = document.getElementById('msgzone'); msgzone.innerHTML += rep; document.getElementsByTagName('textarea')[0].value = ''; } } } xhr.send('rec='+rec+'&content='+content); } </script> <style> #msgzone{ border:1px solid #ececec; width:500px; height:500px; overflow:scroll; } </style> </head> <body> <h1>在线客服系统--客服端</h1> <!-- 消息显示区域 --> <div id="msgzone"></div> <!-- 用户名区域 --> 回复:<span id="rec"></span><br/> <!-- 回复内容区域 --> <textarea> </textarea><input type="button" value="回复" onclick="huifu();" /> <!-- 隐藏的iframe,其中加载了query.php来不断查询数据库是否有要推送的消息 --> <iframe src="query.php" width="0" height="0" iframeBorder="0"></iframe> </body></html> 后端查询query.php代码:<?phpset_time_limit(0);ob_start();echo str_repeat(' ',4000),'<br/>';ob_flush();flush();$i = 0;require 'conn.php';//连接数据库文件//不断循环查询消息while(true){ //查询接收者为admin,并且是未读的消息 $sql = 'select * from msg where rec = "admin" and isread = 0 limit 1'; $result = $conn -> query($sql); //提取查询结果 $rs = $result -> fetch_assoc(); if(!empty($rs)){ //将这条消息更新为已读状态 $sql = 'update msg set isread = 1 where id = "'.$rs['id'].'" limit 1'; $conn -> query($sql); //转换为JSON格式准备发送给JS $rs = json_encode($rs); //通过paren调用父窗体的comet()函数,并将转换的JSON数据传送过去,comet()函数可以在kefu.html代码中找到 echo '<script>'; echo 'parent.window.comet(',$rs,',',mt_rand(1,1000000),')'; echo '</script>'; } ob_flush(); flush(); //防止查询次数过多,设置的每1秒查询一次 sleep(1);} 数据库连接conn.php代码:<?php $conn = new mysqli('localhost','root','jackson613','test'); $conn -> query('set names utf8'); 建议还是采用AJAX的轮询方式,这种的后台查询脚本里没有主动结束脚本执行,可能存在一定问题。 中文字符串对比,含有标点符号, UTF-8转gb遇到?就不转换了 phpmailer发送邮件,使用hotmail邮箱需要开启ssl功能,如何开启 iframe中的传值如何传??? session问题 求教高手PHP截取字符串数量 再来一剂编码问题。。。饿 哪位大哥能把这整个文档下载过来?或者要求高一点做成CHM格式的? 出现错误 Parse error: parse error, unexpected T_PUBLIC 怎样将listbox中的item插入数据库? 请教个不知道算php还是算html的纠结问题... 数组重组
setcookie('username','admin');
?>
<!doctype html>
<html>
<head>
<title>在线客服系统</title>
<meta charset="utf-8" />
<script>
//测试浏览器是Chrome 27.0.1453.93
var xhr = new XMLHttpRequest(); //由query.php程序调用的JS函数,用来输出内容到前台页面
function comet(msg,rand){
var content = '';
//获取用户发送给前台的消息,并拼接好
content = '<span onclick="reply(this);" style="cursor:pointer;">' + msg.pos + ' </span>' + '对你说:<br/> ' + msg.content + '<br/>';
var msgzone = document.getElementById('msgzone');
//把拼接的消息赋值给消息显示区域
msgzone.innerHTML += content;
}
//点击某个用户名,然后回复的span会显示该用户名
function reply(reobj){
document.getElementById('rec').innerHTML = reobj.innerHTML
}
//前台客服回复时,调用的函数
function huifu(){
var rep = '';
//接收前台发送的用户名字
var rec = document.getElementById('rec').innerHTML;
//前台发送的消息内容
var content = document.getElementsByTagName('textarea')[0].value; if(rec == '' || content == ''){
alert('请选择回复人并填写回复内容');//简单判断回复对象和内容不能为空
return;
}
//sendmsg.php是专门配套前台发送消息用的,就是把消息INSERT进数据库
xhr.open('POST','sendmsg.php',true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
if(this.responseText == 'ok'){
//当确定前台发送的消息INSERT进数据库后,把这条消息显示到消息区域内
rep += '你对 ' + rec + '说:<br/> ' + content + '<br/>';
var msgzone = document.getElementById('msgzone');
msgzone.innerHTML += rep;
document.getElementsByTagName('textarea')[0].value = '';
}
}
}
xhr.send('rec='+rec+'&content='+content);
}
</script>
<style>
#msgzone{
border:1px solid #ececec;
width:500px;
height:500px;
overflow:scroll;
}
</style>
</head>
<body>
<h1>在线客服系统--客服端</h1>
<!-- 消息显示区域 -->
<div id="msgzone"></div>
<!-- 用户名区域 -->
回复:<span id="rec"></span><br/>
<!-- 回复内容区域 -->
<textarea>
</textarea><input type="button" value="回复" onclick="huifu();" />
<!-- 隐藏的iframe,其中加载了query.php来不断查询数据库是否有要推送的消息 -->
<iframe src="query.php" width="0" height="0" iframeBorder="0"></iframe>
</body>
</html>
ob_flush();
flush();$i = 0;require 'conn.php';//连接数据库文件
//不断循环查询消息
while(true){
//查询接收者为admin,并且是未读的消息
$sql = 'select * from msg where rec = "admin" and isread = 0 limit 1';
$result = $conn -> query($sql);
//提取查询结果
$rs = $result -> fetch_assoc();
if(!empty($rs)){
//将这条消息更新为已读状态
$sql = 'update msg set isread = 1 where id = "'.$rs['id'].'" limit 1';
$conn -> query($sql);
//转换为JSON格式准备发送给JS
$rs = json_encode($rs); //通过paren调用父窗体的comet()函数,并将转换的JSON数据传送过去,comet()函数可以在kefu.html代码中找到
echo '<script>';
echo 'parent.window.comet(',$rs,',',mt_rand(1,1000000),')';
echo '</script>';
}
ob_flush();
flush();
//防止查询次数过多,设置的每1秒查询一次
sleep(1);
}
$conn = new mysqli('localhost','root','jackson613','test'); $conn -> query('set names utf8');