Home Backend Development PHP Tutorial PHP实现聊天室的主动更新与被动更新_PHP

PHP实现聊天室的主动更新与被动更新_PHP

Jun 01, 2016 pm 12:32 PM
this accomplish renew program chatroom

聊天的内容如何显示在屏幕上,一种是每隔一段时间刷新一次页面,读入全部聊天
内容,然后显示,这里采用的是js的document.write的方法实现不刷新的聊天页面
!

1 主页面的生成,规定了CSS类型,显示欢迎词
function write2(){
if(this.u.document==null)return;
this.u.document.writeln("");
this.u.document.writeln("ml; charset=gb2312>");
this.u.document.writeln("");

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


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

");
}

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

if($action == "enter")
{

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

//发言内容,某某进入聊天室了
$message = "$name
到聊天室".$message." ".date("m月d日 H:i")."<script>parent.add('$name', <br />'$photo');parent.write1();</script>
";
}
//更新发言内容
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){

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

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

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


//锁定发言总数文件
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 主动更新的自动循环调用方法

可以使用&&pass= print($pass);
?>&&last="+winnumber;
flush.location=url
flushflag=false
}

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

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


5 主动更新程序



这样,每个发送的发言,经过被动更新程序处理保存到文件内,然后由一个循环的主
动更新程序完成显示任务!!!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

How to fix Blizzard Battle.net update stuck at 45%? How to fix Blizzard Battle.net update stuck at 45%? Mar 16, 2024 pm 06:52 PM

Blizzard Battle.net update keeps stuck at 45%, how to solve it? Recently, many people have been stuck at the 45% progress bar when updating software. They will still get stuck after restarting multiple times. So how to solve this situation? We can reinstall the client, switch regions, and delete files. To deal with it, this software tutorial will share the operation steps, hoping to help more people. Blizzard Battle.net update keeps stuck at 45%, how to solve it? 1. Client 1. First, you need to confirm that your client is the official version downloaded from the official website. 2. If not, users can enter the Asian server website to download. 3. After entering, click Download in the upper right corner. Note: Be sure not to select Simplified Chinese when installing.

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

Windows cannot access the specified device, path, or file Windows cannot access the specified device, path, or file Jun 18, 2024 pm 04:49 PM

A friend's computer has such a fault. When opening "This PC" and the C drive file, it will prompt "Explorer.EXE Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the project." Including folders, files, This computer, Recycle Bin, etc., double-clicking will pop up such a window, and right-clicking to open it is normal. This is caused by a system update. If you also encounter this situation, the editor below will teach you how to solve it. 1. Open the registry editor Win+R and enter regedit, or right-click the start menu to run and enter regedit; 2. Locate the registry "Computer\HKEY_CLASSES_ROOT\PackagedCom\ClassInd"

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

See all articles