php专区

 首页 > php专区 > PHP进阶 > 网络编程 > 聊天室技术(三) -- 在线人数

聊天室技术(三) -- 在线人数

分享到:
【字体:
导读:
         摘要:我根据网易聊天室的在线人数的方法,显示当前的在线人数,代码解释如下:1登陆时建立在线人名单的数组,放在body后面lt;?//锁定在线人数文件while(file_exists($useronlinelock)){$pppp++;}fclose(fopen($useronlinelock,w));//读入在线人名单$u...

聊天室技术(三) -- 在线人数
我根据网易聊天室的在线人数的方法,显示当前的在线人数,代码解释如下:

 

1 登陆时建立在线人名单的数组,放在body后面

<?
//锁定在线人数文件
while(file_exists($useronlinelock)){$pppp++;}
fclose(fopen($useronlinelock,"w"));

//读入在线人名单
$useronline = file($useronline);
unlink($useronlinelock);

//建立数组 list
print("document.writeln("list=new Array(");
$k=count($useronline);
if($k>1)
{
for($i=0;$i<($k-1);$i++)
{
$usercurrent = split($split,$useronline[$i],99);
// 姓名+,
print("'$usercurrent[0]',");
}
$i=$k-1;
// 处理最后一个姓名
$usercurrent = split($split,$useronline[$i],99);
print("'$usercurrent[0]'");
}
// 数组结束
print(")");n");
?>

2显示在线人数的js
document.writeln('[在线人数<font color=red>'+count+'</font>]<br>');
document.writeln("[<a href="http://www.php1.cn/">a>]<br>");
document.writeln("<font class='p9'>");
var j,name,club;
for(var i=0;i<list.length;i=i+1)
{
if(list[i]!=null){

//显示每个在线人的名字
document.writeln("<a href="http://www.php1.cn/">e='"+list[i]+"'>"+list[i]+"</a><br>");
}
}
this.r.document.writeln('</font><hr>');


3改变聊天对象
function cs(name)
{
if(this.d.document==null)return;
if(name=='所有人')
{
this.d.add('所有人');
this.d.document.inputform.talkto.value='所有人';

//改变焦点
this.d.document.inputform.msg.focus();
return;
}
for(var i=0;i<list.length;i=i+1)
{
if(list[i]==name)
{

//更改发送的谈话对象
this.d.document.inputform.talkto.value=list[i];
this.d.document.inputform.msg.focus();
return;
}
}

//错误
alert('此用户已离线或已改了昵称。');
}

4删除一个用户
function del(str)
{
for(var i=0;i<list.length;i=i+1)
if(list[i]==str)
{
delete list[i];
count--;
}
}


5增加一个用户
function add(str1,str2)
{
var l=list.length;
for(var i=0;i<list.length;i=i+1)

//如果已经在数组里面则返回
if(list[i]==str1)
return;

//增加一个用户
list[l]=str1;
count++;
}

6更新聊天人数的方法,定时器的使用
var timerID=null;
var timerRunning=false;

function stop()
{
//停止
if(timerRunning)clearTimeout(timerID);
timerRunning=false;
}
function start()
{
stop();
//调用更新在线人数的程序
write1();
}

function write1()
{
... ... ... ...
//设定更新时间,
timerID=setTimeout("start()",30000);
timerRunning=true;
}


这种方法比较简单的实现了在线人数的显示,当然也可以使用读入在线人文件的方法显示在线人数,不过在改变聊天对象是会比较麻烦.

原作者:howtodo
来源:php2000.com

聊天室技术(三) -- 在线人数
分享到:
聊天室技术(四)-- 显示
聊天室技术(四)-- 显示现在的www聊天室基本全部采用框架方式,可以用frame也可以用iframe看个人喜欢了,我的采用frame的传统方式   print("&lt;frameset rows="*,110,0,0,0" border=0&gt;n"); print("&lt;frameset cols="660,118" rows="*"&gt;n"); //主显示屏幕,负责显示聊天内容 print("&lt;frame name=u src=about:b...
聊天室技术(二)-- 登录
聊天室技术(二)-- 登录1 页面登陆幕?疽??你可以在我的竹叶看到登陆的表单,这里提供了最基本的登陆表单项 (1)登陆表单 &lt;form method=POST name=chatform action=chat/login.php?action=enter onSubmit="b1_submit();return true;" target="howtodo"&gt; (a)聊天表单的名字为chatform,我使用action=enter作为进入聊天...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……