代码改变世界

php+js+mysql设计的仿webQQ-<7>聊天消息的发送与刷新

2012-05-13 20:24  java线程例子  阅读(205)  评论(0编辑  收藏  举报

<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文件

<?php
session_start();
header('Content-Type:text/html;charset=GB2312');
include("conn.php");
$email=$_SESSION["email"];
$nickname=$_SESSION["nickname"];
$friendEmail=$_GET["friendEmail"];
$text=$_GET["text"];
$sendtime=$_GET["sendtime"];
$sql="insert into chats values(now(),'$email','$friendEmail','$text',0)";
if($result=mysql_query($sql))
  { 
    echo "<font color='BLUE'>".$nickname." [".$sendtime."]</font><br/>";
	echo $text;
	echo "<br/>";
  }
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文件

<?php
session_start();
header('Content-Type:text/html;charset=GB2312');
include("conn.php");
$email=$_SESSION["email"];
$friendEmail=$_GET["friendEmail"];
$sql="select nickname,message,chattime from chats,user where chats.speaker=user.email and speaker='".$friendEmail."' and rapper='".$email."' 
and messageState=0";
if($result=mysql_query($sql))
{   
	while($row=mysql_fetch_array($result))
		 {
			 echo "<font color='BLUE'>".$row["nickname"]." [".$row["chattime"]."]"."</font><br/>";
			 echo $row["message"];
			 echo "<br/>";
		 }
    $updateSQL="update chats set messageState=1 where speaker='".$friendEmail."' and rapper='".$email."'";
	mysql_query($updateSQL);
 }
?>

至此,关键的技术点都写出来啦,欢迎大家拍砖!(完)