当前位置:首页> PHP教程> 经典文章
关键字
文章内容
PHP实现聊天室的主动更新与被动更新
 
 
修改时间:[2008/08/13 17:44]    阅读次数:[921]    发表者:[起缘]
 
聊天的内容如何显示在屏幕上,一种是每隔一段时间刷新一次页面,读入全部聊天
内容,然后显示,这里采用的是js的document.write的方法实现不刷新的聊天页面
!

1 主页面的生成,规定了CSS类型,显示欢迎词
function write2(){
if(this.u.document==null)return;
this.u.document.writeln("<html><head>");
this.u.document.writeln("<meta http-equiv=Content-Type content=text/ht
ml; charset=gb2312>");
this.u.document.writeln("<style type=text/css>");
this.u.document.writeln(".p9 { font-size: 11pt; line-height: 15pt}");

this.u.document.writeln("body { font-size: 11pt; line-height: 15pt}");

this.u.document.writeln("a:visited { font-size: 11pt;color: ext-decoration: none;}");
this.u.document.writeln("a:link { font-size: 11pt;color: -decoration: none}");
this.u.document.writeln("a:hover { font-size: 11pt;color:
this.u.document.writeln("</style>");

this.u.document.writeln("</head>");
this.u.document.writeln("<body);
//.................. 这里插入生成在线人数组的程序段


this.u.document.writeln("<script>");
this.u.document.writeln("<p class=p9 align=left>");
this.u.document.writeln("<p align=center>欢迎光临PlayBoy聊天室,本聊天室
正在测试阶段,如有问题请与<a href=mailto:pccastle@sina.com>我们联系</a>
</p>");
}

2 初始化进入信息,第一次进入聊天室

if($action == "enter")
{

/////////////////// 调用显示主屏幕的js程序 ////////////////////
print("parent.write2();\n");

//发言内容,某某进入聊天室了
$message = "<a href=javascript:parent.cs('$name'); target=d>$name</a>来
到聊天室".$message." ".date("m月d日 H:i")."<script>parent.add('$name',
'$photo');parent.write1();<\/script><br>";
}
//更新发言内容
while(file_exists($lockfile)){ $pppp++; }

//发言的锁定
fclose(fopen($lockfile,"w"));

//读入发言的总句数,也就是所有人一共发了多少言!我们可以保存每一个发言,但
是这样会占用大量的磁盘空间,我们采用了一种取模的方法,循环使用文件来减少
文件操作!
$talkmessage = file($filename);
$number = chop($talkmessage[0]);

//发言数增加一,然后保存
$talkhandle = fopen($filename,"w");
$number++;
fputs($talkhandle,$number);
fclose($talkhandle);

/去掉锁定
unlink($lockfile);

//对发言总数对10取模,作为文件名保存发言内容,也就是说第11句和第1句使用同
一个文件名,由于不可能同时有10句话没有更新,所以这是数在人不是非常多的情
况下很好!当然,考虑到人多的情况,可以设成100.
$filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);

//显示进入信息
print("parent.u.document.writeln(\"$message\");\n");

//调用主动刷新js程序,传递已经显示的发言数目
print("parent.flushwin($number)\n");

//保存最后一次显示的发言
$last = $number;
}


3 处理发送表单的请求

//不处理空的发言和超过一定数目的发言
if( ($message != "")&&(strlen($message)<150))
{

//检查发言者是否在线,防止意外
$onlineperson = file("useronline.dbf");
$personsign=0;
for($i=0;$i<count($onlineperson);$i++)
{
$person = split($split,$onlineperson[$i],99);
if($person[0] == $name)
{
$personsign = 1;
$person[3] = date("U");
break;
}
}

//在线时的处理程序
if($personsign == 1)
{

//添加发言时间的部分
$message = $message." <font size=1>".date("m月d日 H:i")."</font><br>";


//锁定发言总数文件
while(file_exists($lockfile)){ $pppp++; }
fclose(fopen($lockfile,"w"));

//读入发言总数
$talkmessage = file($filename);
$number = chop($talkmessage[0]);

//总数加1,然后保存
$talkhandle = fopen($filename,"w");
$number++;
fputs($talkhandle,$number);
fclose($talkhandle);
unlink($lockfile);

//总数对10取模后以文件形式保存发言内容
$filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);
}
}

//////////////////////////////////////////////////////////////////
这样,表单的处理已经完成,下面的主动更新程序将会把新的发言内容显示在屏幕

//////////////////////////////////////////////////////////////////

4 主动更新的自动循环调用方法

可以使用<meta http-equiv="reflesh" content="3;url=messageflush.php?nam
e=<?print($name)?>&&pass=<?print($pass)&&last=<?print($last)?>的方式更
新!

我的程序以前就是使用这种方法自动更新的,但是我发现一个问题,那就是当这个
更新程序出现运行错误时,他不会产生调用下次更新的代码,造成后台更新程序停
止工作!所以我采用了js定时的方法来完成同样的功能!

var flushtimeID=null;
var flushRunning=false;

//上次更新标志
var flushflag = true;

function flushstop()
{
if(flushtimerRunning)clearTimeout(flushtimerID);
flushtimerRunning=false;
}
function flushstart()
{
flushstop();

//使用发送表单里面的上次显示的值
flushwin(this.d.document.inputform.last.value);
}

function flushwin(winnumber)
{
//如果上次更新正确,则调用下次更新
if(flushflag == true)
{
url="messageflush.php?name=<? print($name); ?>&&pass=<? print($pass);
?>&&last="+winnumber;
flush.location=url
flushflag=false
}

//否则等待一个循环
flushtimerID=setTimeout("flushstart()",2000);
flushtimerRunning=true;
}

这种方法保证了在主程序不死的情况下,后台更新程序会一直运行下去!


5 主动更新程序
<script Language='JavaScript'>
<?
//读入最大的发言数目
$message = file($filename);
$number = chop($message[0]);

//从上次显示的下一个发言开始到最大发言结束,显示发言内容
for($i=$last+1;$i<=$number;$i++)
{
//读入下一个发言内容
$filename = "messageonline".($i%10).".php";
$message = file($filename);
$tempmessage = split($split,$message[0],99);

//显示发言内容
print("parent.u.document.writeln(\"$message[0]\");\r\n");
}

//更新发送表单最后一个发言的数目
print("parent.d.document.inputform.last.value=$number;\n");

//通知主程序本次更新已经完成
print("parent.flushflag=true;\n");
?>
</script>


这样,每个发送的发言,经过被动更新程序处理保存到文件内,然后由一个循环的主
动更新程序完成显示任务!!!