|
聊天的內(nèi)容如何顯示在屏幕上,一種是每隔一段時(shí)間刷新一次頁面,讀入全部聊天 內(nèi)容,然后顯示,這里采用的是js的document.write的方法實(shí)現(xiàn)不刷新的聊天頁面 !
1 主頁面的生成,規(guī)定了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); //.................. 這里插入生成在線人數(shù)組的程序段
this.u.document.writeln("<script>"); this.u.document.writeln("<p class=p9 align=left>"); this.u.document.writeln("<p align=center>歡迎光臨PlayBoy聊天室,本聊天室 正在測(cè)試階段,如有問題請(qǐng)與<a href=mailto:pccastle@sina.com>我們聯(lián)系</a> </p>"); }
2 初始化進(jìn)入信息,第一次進(jìn)入聊天室
if($action == "enter") {
/////////////////// 調(diào)用顯示主屏幕的js程序 //////////////////// print("parent.write2();n");
//發(fā)言內(nèi)容,某某進(jì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>"; } //更新發(fā)言內(nèi)容 while(file_exists($lockfile)){ $pppp++; }
//發(fā)言的鎖定 fclose(fopen($lockfile,"w"));
//讀入發(fā)言的總句數(shù),也就是所有人一共發(fā)了多少言!我們可以保存每一個(gè)發(fā)言,但 是這樣會(huì)占用大量的磁盤空間,我們采用了一種取模的方法,循環(huán)使用文件來減少 文件操作! $talkmessage = file($filename); $number = chop($talkmessage[0]);
//發(fā)言數(shù)增加一,然后保存 $talkhandle = fopen($filename,"w"); $number++; fputs($talkhandle,$number); fclose($talkhandle);
/去掉鎖定 unlink($lockfile);
//對(duì)發(fā)言總數(shù)對(duì)10取模,作為文件名保存發(fā)言內(nèi)容,也就是說第11句和第1句使用同 一個(gè)文件名,由于不可能同時(shí)有10句話沒有更新,所以這是數(shù)在人不是非常多的情 況下很好!當(dāng)然,考慮到人多的情況,可以設(shè)成100. $filehandle = fopen("messageonline".($number%10).".php","w"); fputs($filehandle,$message); fclose($filehandle);
//顯示進(jìn)入信息 print("parent.u.document.writeln("$message");n");
//調(diào)用主動(dòng)刷新js程序,傳遞已經(jīng)顯示的發(fā)言數(shù)目 print("parent.flushwin($number)n");
//保存最后一次顯示的發(fā)言 $last = $number; }
3 處理發(fā)送表單的請(qǐng)求
//不處理空的發(fā)言和超過一定數(shù)目的發(fā)言 if( ($message != "")&&(strlen($message)<150)) {
//檢查發(fā)言者是否在線,防止意外 $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; } }
//在線時(shí)的處理程序 if($personsign == 1) {
//添加發(fā)言時(shí)間的部分 $message = $message." <font size=1>".date("m月d日 H:i")."</font><br>";
//鎖定發(fā)言總數(shù)文件 while(file_exists($lockfile)){ $pppp++; } fclose(fopen($lockfile,"w"));
//讀入發(fā)言總數(shù) $talkmessage = file($filename); $number = chop($talkmessage[0]);
//總數(shù)加1,然后保存 $talkhandle = fopen($filename,"w"); $number++; fputs($talkhandle,$number); fclose($talkhandle); unlink($lockfile);
//總數(shù)對(duì)10取模后以文件形式保存發(fā)言內(nèi)容 $filehandle = fopen("messageonline".($number%10).".php","w"); fputs($filehandle,$message); fclose($filehandle); } }
////////////////////////////////////////////////////////////////// 這樣,表單的處理已經(jīng)完成,下面的主動(dòng)更新程序?qū)?huì)把新的發(fā)言內(nèi)容顯示在屏幕 上 //////////////////////////////////////////////////////////////////
4 主動(dòng)更新的自動(dòng)循環(huán)調(diào)用方法
可以使用<meta http-equiv="reflesh" content="3;url=messageflush.php?nam e=<?print($name)?>&&pass=<?print($pass)&&last=<?print($last)?>的方式更 新!
我的程序以前就是使用這種方法自動(dòng)更新的,但是我發(fā)現(xiàn)一個(gè)問題,那就是當(dāng)這個(gè) 更新程序出現(xiàn)運(yùn)行錯(cuò)誤時(shí),他不會(huì)產(chǎn)生調(diào)用下次更新的代碼,造成后臺(tái)更新程序停 止工作!所以我采用了js定時(shí)的方法來完成同樣的功能!
var flushtimeID=null; var flushRunning=false;
//上次更新標(biāo)志 var flushflag = true;
function flushstop() { if(flushtimerRunning)clearTimeout(flushtimerID); flushtimerRunning=false; } function flushstart() { flushstop();
//使用發(fā)送表單里面的上次顯示的值 flushwin(this.d.document.inputform.last.value); }
function flushwin(winnumber) { //如果上次更新正確,則調(diào)用下次更新 if(flushflag == true) { url="messageflush.php?name=<? print($name); ?>&&pass=<? print($pass); ?>&&last="+winnumber; flush.location=url flushflag=false }
//否則等待一個(gè)循環(huán) flushtimerID=setTimeout("flushstart()",2000); flushtimerRunning=true; }
這種方法保證了在主程序不死的情況下,后臺(tái)更新程序會(huì)一直運(yùn)行下去!
5 主動(dòng)更新程序 <script Language='JavaScript'> <? //讀入最大的發(fā)言數(shù)目 $message = file($filename); $number = chop($message[0]);
//從上次顯示的下一個(gè)發(fā)言開始到最大發(fā)言結(jié)束,顯示發(fā)言內(nèi)容 for($i=$last+1;$i<=$number;$i++) { //讀入下一個(gè)發(fā)言內(nèi)容 $filename = "messageonline".($i%10).".php"; $message = file($filename); $tempmessage = split($split,$message[0],99);
//顯示發(fā)言內(nèi)容 print("parent.u.document.writeln("$message[0]");rn"); }
//更新發(fā)送表單最后一個(gè)發(fā)言的數(shù)目 print("parent.d.document.inputform.last.value=$number;n");
//通知主程序本次更新已經(jīng)完成 print("parent.flushflag=true;n"); ?> </script>
這樣,每個(gè)發(fā)送的發(fā)言,經(jīng)過被動(dòng)更新程序處理保存到文件內(nèi),然后由一個(gè)循環(huán)的主 動(dòng)更新程序完成顯示任務(wù)!!!
|