<12>消息的发送
Js文件
function sendMessage(friendEmail,chat,text){
var xmlhttp;
if(window.XMLHttpRequest){//code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if(xmlhttp.overrideMimeType){//设置MIME类别
xmlhttp.overrideMimeType("text/xml");
}
}
else{//code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var today=new Date(); //获取本地时间
var y=today.getFullYear();
var mo=today.getMonth()+1; //因为js获取本地时间的月份的值的范围是0-11,所以在这里加1
var d=today.getDate();
var h=today.getHours();
var mi=today.getMinutes();
var s=today.getSeconds();
var sendtime=y+"-"+mo+"-"+d+" "+h+":"+mi+":"+s;
var url="sendMessage.php?friendEmail="+friendEmail.id+"&text="+text+"&sendtime="+sendtime; //转到php文件来处理消息
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(chat).innerHTML+=xmlhttp.responseText;
var e=document.getElementById(chat);
e.scrollTop=e.scrollHeight; //该语句使滚动条永远在最下面
}
}
xmlhttp.send(null);
}
PHP文件
".$nickname." [".$sendtime."]
";
echo $text;
echo "
";
}
else
{
echo "消息写入数据库失败!";
}
?>
<13>消息的刷新
Js文件
function refreshChatDiv(obj,chatDivID){
var xmlhttp;
if(window.XMLHttpRequest){//code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if(xmlhttp.overrideMimeType){//设置MIME类别
xmlhttp.overrideMimeType("text/xml");
}
}
else{//code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var url="refreshChatDiv.php?friendEmail="+obj.id; //转到PHP文件去查询数据库中有没有新的消息
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(chatDivID).innerHTML+=xmlhttp.responseText;
var e=document.getElementById(chatDivID);
e.scrollTop=e.scrollHeight;
setTimeout(function() {refreshChatDiv(obj,chatDivID);},3000); //每隔3秒检测一下有没有新的信息
}
}
xmlhttp.send(null);
}
PHP文件
".$row["nickname"]." [".$row["chattime"]."]"."
";
echo $row["message"];
echo "
";
}
$updateSQL="update chats set messageState=1 where speaker='".$friendEmail."' and rapper='".$email."'";
mysql_query($updateSQL);
}
?>
至此,关键的技术点都写出来啦,欢迎大家拍砖!(完)