Chat room Technology (III)-number of online users

Source: Internet
Author: User
According to the NetEase chat room's online users method, the current number of online users is displayed. the code is explained as follows: 1. an array of online users is created during login and placed behind the body. lt ;? // Lock the number of online users file while (file_exists ($ useronlinelock) {$ pppp ++;} fclose (fopen ($ useronlinelock, w )); // read the online user list $ u. based on the number of online users in the NetEase chat room, the current number of online users is displayed. the code is as follows:

 

1. create an array of online person lists during login and place them behind the body.

// Lock the number of online users file
While (file_exists ($ useronlinelock) {$ pppp ++ ;}
Fclose (fopen ($ useronlinelock, "w "));

// Read the online person list
$ Useronline = file ($ useronline );
Unlink ($ useronlinelock );

// Create an array 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 );
// Name +,
Print ("'$ usercurrent [0]',");
}
$ I = $ K-1;
// Process the last name
$ Usercurrent = split ($ split, $ useronline [$ I], 99 );
Print ("'$ usercurrent [0]'");
}
// Array ends
Print (")"); n ");
?>

2. js showing the number of online users
Document. writeln ('[online users' + count + ']
');
Document. writeln ("[All A>]
");
Document. writeln ("");
Var j, name, club;
For (var I = 0; I {
If (list [I]! = Null ){

// Display the name of each online person
Document. writeln ("e = '" + list [I] + "'>" + list [I] +"
");
}
}
This.r.doc ument. writeln ('');


3. change the chat object
Function cs (name)
{
If(this.d.doc ument = null) return;
If (name = 'Everybody ')
{
This. d. add ('Everybody ');
This.d.doc ument. inputform. talkto. value = 'allow ';

// Change focus
This.d.doc ument. inputform. msg. focus ();
Return;
}
For (var I = 0; I {
If (list [I] = name)
{

// Change the sent conversation object
This.d.doc ument. inputform. talkto. value = list [I];
This.d.doc ument. inputform. msg. focus ();
Return;
}
}

// Error
Alert ('The user has been offline or changed the nickname. ');
}

4. delete a user
Function del (str)
{
For (var I = 0; I If (list [I] = str)
{
Delete list [I];
Count --;
}
}


5. add a user
Function add (str1, str2)
{
Var l = list. length;
For (var I = 0; I

// If it is already in the array, return
If (list [I] = str1)
Return;

// Add a user
List [l] = str1;
Count ++;
}

6. update the number of chat users and use the timer.
Var timerID = null;
Var timerRunning = false;

Function stop ()
{
// Stop
If (timerRunning) clearTimeout (timerID );
TimerRunning = false;
}
Function start ()
{
Stop ();
// Call the program to update the number of online users
Write1 ();
}

Function write1 ()
{
............
// Set the update time,
TimerID = setTimeout ("start ()", 30000 );
TimerRunning = true;
}


This method is relatively simple to display the number of online users. of course, you can also use the method of reading the online person file to display the number of online users. However, it is troublesome to change the chat object.

Original Author: howtodo
Source: php2000.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.