聊天的內容如何顯示在屏幕上,一種是每隔一段時間刷新一次頁面,讀入全部聊天 內容,然后顯示,這里采用的是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]");rn"); }
//更新發送表單最后一個發言的數目 print("parent.d.document.inputform.last.value=$number;n");
//通知主程序本次更新已經完成 print("parent.flushflag=true;n"); ?> </script>
這樣,每個發送的發言,經過被動更新程序處理保存到文件內,然后由一個循環的主 動更新程序完成顯示任務!!!
|