//online.cs (User Online Detection) / * Program Implementation: Name: User Name SessionID: User ID, through it means an user iswhere: Additional information, the user is current location lasttime: User Logging Time CURTIME: This refresh time is in the client, using an IFRAME, load a refresh page, update the CURTIME corresponding to his name every XX second, means he is still in the server, establish a daemon, each Run again across the fixed time, then determine if the time interval in all user lists exceeds the specified time, if it is exceeded, then remove the user from the online list, so you can detect whether the user is online, if If you write a user from the process after you write a user, you can solve a lot of people asked: the user's unexpected hanging line.
* / # Define DEBUGusing System; using System.Data; using System.Data.SqlClient; using System.Collections; using System.Threading; using System.Web; using System.Diagnostics; namespace SohoProject {// definition of the structure of a public struct User {public string name; public DateTime lasttime; public DateTime curtime; public string sessionid; public string ip; public string iswhere;} public class OnLineUser {private static DataTable _alluser; // read-only properties public DataTable alluser {get {return _alluser; }} public onLineUser () {if (_alluser == null) {// define user list // Declare variables for DataColumn and DataRow objects _alluser = new DataTable ( "onlineuser");. DataColumn myDataColumn; // Create new DataColumn, set DataType, ColumnName and add to DataTable myDataColumn = new DataColumn ();. myDataColumn.DataType = System.Type.GetType ( "System.String"); myDataColumn.ColumnName = "name"; myDataColumn.AutoIncrement = false; myDataColumn.Cap tion = "name"; myDataColumn.ReadOnly = false; myDataColumn.Unique = false; _alluser.Columns.Add (myDataColumn); // Create sessionid column myDataColumn = new DataColumn ();. myDataColumn.DataType = System.Type.GetType ( "System.String"); myDataColumn.ColumnName = "sessionid"; myDataColumn.AutoIncrement = false; myDataColumn.Caption = "sessionid"; myDataColumn.ReadOnly = false; myDataColumn.Unique = true; _alluser.Columns.Add (myDataColumn); // Create IP column. Mydatacolumn = new datacolumn (); mydatacolumn.dattype = system.type.gettype ("system.string");
myDataColumn.ColumnName = "ip"; myDataColumn.AutoIncrement = false; myDataColumn.Caption = "ip"; myDataColumn.ReadOnly = false; myDataColumn.Unique = false; _alluser.Columns.Add (myDataColumn);. // Create iswhere column myDataColumn = new DataColumn (); myDataColumn.DataType = System.Type.GetType ( "System.String"); myDataColumn.ColumnName = "iswhere"; myDataColumn.AutoIncrement = false; myDataColumn.Caption = "iswhere"; myDataColumn.ReadOnly = false ; myDataColumn.Unique = false; _alluser.Columns.Add (myDataColumn);. // Create iswhere column myDataColumn = new DataColumn (); myDataColumn.DataType = System.Type.GetType ( "System.DateTime"); myDataColumn.ColumnName = "lasttime"; myDataColumn.AutoIncrement = false; myDataColumn.Caption = "lasttime"; myDataColumn.ReadOnly = false; myDataColumn.Unique = false; _alluser.Columns.Add (myDataColumn);. // Create iswhere column myDataColumn = new DataColumn ( ); Mydatacolumn.datat ype = System.Type.GetType ( "System.DateTime"); myDataColumn.ColumnName = "curtime"; myDataColumn.AutoIncrement = false; myDataColumn.Caption = "curtime"; myDataColumn.ReadOnly = false; myDataColumn.Unique = false; _alluser .COLUMNS.ADD (MyDataColumn);}} // Function Description: Add the current user to the online list // (User user) {#if debug (new sohoproject.sohodebug ()). WritetoDoc ("Started to enter the current user online list> ...."); (new sohoproject.sohodebug ()). Writetodoc ("" );
#ENDIF // Start Search has existed the user, if there is a change data, otherwise add a new user string strexpr; strexpr = "sessionid = '" user.sessionID "'"; dataroow [] curuser; // Use the select method to find all rows matching the filter. # IF debug (new sohoproject.sohodebug ()). WritetoDoc ("Search String:" strexpr); (New sohoproject.sohodebug ()). Writetodoc ("") ; #endif curUser = _alluser.Select (strExpr);. # if DEBUG (new SohoProject.SohoDebug ()) WriteToDoc (strExpr); (new SohoProject.SohoDebug ()) WriteToDoc (curUser.Length.ToString ());. # Endif if (int i = 0; I // use the select method to find all rows matching the filter. Curuser = _alluser.select (strexpr); if (curuser.Length> 0) {Return true;} else {return false;}} // function Description: Update User Online Time // Return Value: Latest Online User List Public Boolean CheckUseronline (String Name, String Iswhere, String SessionID, String IP) {#if debug (new sohoproject.sohodebug ()). WritetoDoc ("Start to check the user method. ... "); (new sohoproject.sohodebug ()). WriteTodoc (" "); # ENDIF // Need to determine if the user has already in the user list User newuser = new user (); newuser.name = name; newuser.iswhere = iswhere; newuser.lasttime = newuser.curtime = DateTime.Now; newuser.sessionid = sessionid; newuser.ip = ip; OnLineUser alluser = new OnLineUser (); alluser.AddUserToOnLine (newuser); # if DEBUG (new SOHOPROJECT.SOHODEBUG ()). WritetoDoc ("Leave Check User Method ...."); # endif rett}} // Define OnlineUser_old {private static arraylist _lluser; // Define User Public ArrayList Alluser {Get {return_alluser; Set {_alluser = value;}} public onlineuser_old () // Constructor {i (_alluser == null) {_alluser = new arraylist ();}} // function Description: Add the current user to the online list // If the user The data is still in the list of online lists, then temporarily do not let the user log in, prompting the user where the public bool addUsertoonline (user user) {// Need to judge whether the user has in the user list IF (_alluser == null) { _alluser.add (user); return (true);} else {for (int i = 0; i <_ALLUSER.COUNT; i ) {// loop to determine if the user already exists SOHOPROJECT.USER TEMPUSER = (SOHOPROJECT.USER _ALLUSER [I]; IF (Tempuser.SessionId.equals (user.sessionID)) {// Update User online Time Tempuser.name = User.Name; Tempuser.curtime = DATETIME.NOW; TEMPUSER.ISWHERE = User.iswhere; Tempuser.SessionID = User. SessionID; Tempuser.ip = user.ip; alluser [i] = tempuser; return (true); // Return (TRUE); // The user already exists, directly exit}} _lluser.add (user); Return (True );}} // function description: Judgment whether a user is online, this section is not // return value: True is online, false is not public boolean isuseronline (String name) {// Need to judge whether the user is already in the user list IF (_alluser == null) {return (false);} else {for (int i = 0; i <_ALLUSER.COUNT; i ) {// loop to determine if the user already exists sohoproject.user tempuser = (SOHOPROJECT .User_alluser [i]; if (Tempuser.Name.tolower (). Equals (name.tolower ())) {Return (true);}} return (false);}}} // Function Description: Update User Online Time // Return Value: Latest Online User List Public Boolean CheckUseronline (String Name, String Iswhere, String SessionID, String IP) {// Need to first determine if the user has already in the user list IF (! _alluser = null) {User newuser = new User (); newuser.name = name; newuser.iswhere = iswhere; newuser.lasttime = newuser.curtime = DateTime.Now; newuser.sessionid = sessionid; newuser.ip = ip // onlineuser (); addUserToonline ();} returnonline (newuser);} Return (false);}} / * The following starts to establish a guard cable class: (Note: Here, I started writing when I started writing of, However, because many problems have not been done before, there has been a lot of problems, and finally decided to give up and use existing formats) * / public class checkonline {const delay_times = 10000; // Define the time interval for 5 seconds const Int DELAY_SECONDS = 60; // Set the user dropped time to 30 seconds private thread thread; // Defines internal thread private static bool _flag = false; // Define unique logo public checkonline () {i (! _Flag) {_flag = true ; this.thread = new Thread (new ThreadStart (ThreadProc)); thread.Name = "online user"; thread.Start ();}} internal void ThreadProc () {while (true) {// SohoProject.OnLineUser temp = New sohoproject.onlineuser (); // Define a user object // for (int i = 0; i