ASP.NET implements user online detection class source code

xiaoxiao2021-03-06  70

//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.Caption = "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.DataType = 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 (); MyData Column.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.DataType = 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: Add the current online users list // If the user's data is currently in the list of online, the user will not be logged in, prompting the user to exist public bool address {#if debug (new sohoproject.sohodebug ()). Writetodoc ("Start enter < Add the current user online list> .... "); (New sohoproject.sohodebug ()). WriteTodoc (" / r / n "); # ENDIF // Starting Search If the user already exists, if there is a change Data, otherwise add new users string strexpr; strexpr = "sessionid = '" user.ssionid "" "DATAROW [] curuse; // use the select method to find all rows matching the filter. # IF debug (new SOHOPROJECT.SOHODEBUG ()). Writetodoc ("Search String:" STREXPR); (New Sohoproject.sohodebug ()). WriteTodoc ("/ r / n"); # endif curuser = _lluser.select (strexpr); # IF debug (new sohoproject.sohodebug ()). WritetoDoc (strets); (New sohoproject.sohodebug ()). Writetod Oc (curuser.Length.Tostring ()); # endif if (curuser.length> 0) {for (int i = 0; i

User.iswhere; myrow ["lasttime"] = user.lasttime; myrow ["curtime"] = datetime.now; myrow ["sessionID"] = user.sessionID; _alluser.Rows.add (myrow);} catch (Exception e) {throw (new Exception (e "--------------------" e.ToString ()));}} _alluser.AcceptChanges (); return true;} // function: determining whether a user is online, being not part of the present // return value: TRUE representative of line, FALSE is not public Boolean IsUserOnLine (string name) {// need to first determine whether the user has a list of the user // Start whether the search already exists, if there is a change data, otherwise add a new user string strexpr; strexpr = "name = '" Name "'"; DATAROW [] Curuser; // use the select method Method to find all rows matching the filter curUser = _alluser.Select (strExpr); if (curUser.Length> 0) {return true;} else {return false;}} // function: update the time online users // return value : Latest online User List Public Boolean CheckUseronl ine (string name, string iswhere, string sessionid, string ip) {#if DEBUG (new SohoProject.SohoDebug ()) WriteToDoc ( "checking the user entered method ....");. (new SohoProject.SohoDebug ()) .WriteToDoc ( ""); # endif // need to determine whether the user has user newuser in the user list = 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 return true;}} // Define online user class public class onlineuser_old {private static arraylist _alluser; // Define user public arraylist alluser {get {return_alluser;} set {_alluser = value;}} public onlineuser_old () // Constructor {if (_alluser == null) {_lluser = new arraylist ();}} // function Description: Add the current user to the line list // If the user's data is currently Still in the list of online, you will not let the user will log in, prompting the user to exist public bool address {// Need to determine if the user has in the user list IF (_alluser == null) {_lluser.add (User); Return (True);} else {for (int i = 0; i <_allu Ser.count; i ) {// loop judgment whether 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.sswhere; Tempuser.SessionId; Tempuser.ip = User.ip; Alluser [i] = tempuser; return (TRUE); // Return (TRUE); // The user already exists, then 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 in public boolean isuseronline (String name) {// Need to judge whether the user is already IF (_alluser == null) {return (false);} else = 0; I <_ALLUSER.COUNT; i ) {// loop to determine if the user has 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: The Latest Online User List Public Boolean CheckUseronline (String Name, ST Ring iswhere, string sessionid, string ip) {// Need to 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 alluser = new OnLineUser (); AddUserToOnLine (newuser);} return (false);}} / * Here is the establishment of a guardian thread class: (Note: Here, when you start writing, I originally want to make a single piece mode.

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 转载请注明原文地址:https://www.9cbs.com/read-121123.html


New Post(0)