PHP實現聊天室的主動更新與被動更新

來源:互聯網
上載者:User

簡介:這是PHP實現聊天室的主動更新與被動更新的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=324437' scrolling='no'>

聊天的內容如何顯示在螢幕上,一種是每隔一段時間重新整理一次頁面,讀入全部聊天
內容,然後顯示,這裡採用的是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>

這樣,每個發送的發言,經過被動更新程式處理儲存到檔案內,然後由一個迴圈的主
動更新程式完成顯示任務!!!

“PHP實現聊天室的主動更新與被動更新”的更多相關文章 》

  • php|基於反向 Proxy的Web緩衝加速―可快取的CMS系統設計
  • php|網站加速 PHP 緩衝的免費實現方法
  • php|PHP實現聊天室的主動更新與被動更新
  • php|Linux 下 PHP 串連 MS SQLServer 的辦法
  • php|PHP如何產生靜態html
  • php|採集的實現
  • php|PHP字串小常識
  • php|socket_strerror
  • php|JS:正則將首字單詞轉成大寫
  • php|輕鬆修複Discuz!資料庫

PHP實現聊天室的主動更新與被動更新來源網路,如有侵權請告知,即處理!

愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具

http://biancheng.dnbcw.info/php/324437.html pageNo:14

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.