The following is my class file,
//online.cs (User Online Detection)
/*Program implementation ideas:
The user has the following attributes:
Name: Username
sessionid: User ID, the only shows a user through it
Iswhere: additional information, where the user is currently located
LastTime: User login time
curtime: refresh time this time
On the client, using an iframe, loading a refresh page, and updating the Curtime corresponding to his name every XX seconds, it means that he is still in
On the server side, build a guardian thread, run once every fixed time, and then determine whether the time interval in all the current user list is exceeded
If the specified time is exceeded, the user deletes from the online list, so that you can detect whether the user is online, and if it is alone
Write a user offline processing to solve many people asking: the treatment of the user after accidentally hanging the line.
*/
#Define_debug
namespace soholife
{{
using system;
using system.data;
using system.data.sqlclient;
using system.collections;
using system.threading;
using system.web;
using system.diagnostics;
// Define a structure
Public Struct User
{{
public string name;
public datetime lasttime;
public datetime curtime;
Public String sessionid;
public string iswhere;
}
// Define the online user class
public class onlineUSER
{{
Private Static ArrayList_alluser; // Define users
public arraylist alluser
{{
geturn_alluser;}
set {_alluser = value;}
}
public onlineUser () // Construct function
{{
if (_allurs == null)
{{
_alluser = new arraylist ();
}
}
// Function description: Add the current user to the online list
// If the user’s data is still in the online list, it is temporarily not allowed to log in to the user, prompting the user to exist
Public BooladduserToonLine (User User)
{{
// You need to determine whether the user is already in the user list
if (_allurs == null)
{{
_alluser.add (user);
Return (true);
}
else
{{
for (int i = 0; I <_alluser.count; i ++)
{{
// The cycle judgment whether the user already exists
soholife.user tester = (soholife.user) _alluser [i];
If (Tempuser.Sessionid.equals (User.Sessionid) && Tempuser.name.equals (user.name))
{{
Return (false); // The user exists, then exit directly
}
}
_alluser.add (user);
Return (true);
}
}
// Function description: Determine whether a user is online, this part is not used for the time being
// Return value: true represents online, FALSE is not there
PublicBoolean Isauseronline (String name)
{{
// You need to determine whether the user is already in the user list
if (_allurs == null)
{{
Return (false);
}
else
{{
for (int i = 0; I <_alluser.count; i ++)
{{
// The cycle judgment whether the user already exists
soholife.user tester = (soholife.user) _alluser [i];
if (tester.name.equals (name))
{{
Return (true);
}
}
Return (false);
}
}
// Function description: Update user online time
// Return value: the latest online user list
Public Boolean CheckUseronline (String name)
{{
// You need to determine whether the user is already in the user list
if (_alluser! = Null)
{{
for (int i = 0; I <_alluser.count; i ++)
{{
soholife.usertempuser = (soholife.user) _alluser [i];
// First determine whether the current user is yourself
if (tester.name.equals (name))
{{
// Update user online time
tempuser.curtime = datetime.now;
allUSER [i] = Tempuser;
Return (true);
}
}
}
Return (false);
}
}
/*
Let's start establishing a guardian thread class:
(Note: Here, when I started writing
After many problems, I finally decided to give up and use the existing format, but I just had a understanding of a single piece from the happiness. Go back at night
Will use it to write another mode again)
*/
Public Class Checkonline
{{
Const into delay_times = 5000; // Define the time interval of the execution of the execution is 5 seconds
Const into delay_seconds = 30; // Set the user's roll off to 30 seconds
Private Thread Thread; // Define internal threads
Private Static Bool_FLAG = FALSE; // Define the unique logo
Public Checkonline ()
{{
if (! _flag)
{{
_Flag = true;
this.thread = New Thread (New ThreadStart (ThreadProc));
thread.name = online user;
Thread.start ();
}
}
Internet void ThreadProc ()
{{
While (true)
{{
soholife.onlineuseusr test = new soholife.onlineuseuser (); // Define a user object
for (int i = 0; i <temp.alluser.count; i ++)
{{
user tmpuser = (user) temp.alluser [i];
// I added the latest time of the user to 80 seconds, and then compared with the current time, small and current time,
// It means that the user has suspended the line, then his record is deleted
if (TMPUSER.CURTIME.ADDSECONDS (delay_seconds) .Compareto (datetime.now) <0)
{{
temp.alluser.removeat (i);
}
}
Thread.sleep (delay_times);
}
}
}
}
The compilation statement is: CSC /T: Library /out:../bin/online.dll /r: System.dll Online.cs