C # count the number of online members

Source: Internet
Author: User
Tags servervariables

Using System;
Using System. Data;
Using System. Configuration;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. Text;

/// <Summary>
/// Summary of onlineCount
/// </Summary>
Public class onlineCount
{
Private string USERNAME;
Private int OFFLINEDIFF;
Private int REMOVEDIFF;

Public onlineCount ()
{
// Set or read the configuration parameters directly from the configuration file.
/// Session name that stores the user name
USERNAME = HttpContext. Current. Session ["username"]. ToString ();
/// The number of minutes of inactivity deleted from the online list
/// U
OFFLINEDIFF = 5;
/// How many seconds does one delete a non-active user?
REMOVEDIFF = 10;

If (System. Web. HttpContext. Current. Application ["OnlineTalbe"] = null)
{
This. CashTableInit ();
}
}
Public void checkonline ()
{
// Obtain the data table and sessionid from the application
Datatable dtonline;
Dtonline = (datatable) system. Web. httpcontext. Current. application ["onlinetalbe"];
String sessionid = system. Web. httpcontext. Current. session. sessionid. tostring ();
// System. Web. httpcontext. Current. response. Write (sessionid );

// Check whether my records exist in the data table
Datarow drfind = dtonline. Rows. Find (sessionid );
If (drfind! = NULL)
{
// Yes; update my status
Drfind ["lastactivetime"] = datetime. now;
Drfind ["userwhere"] = This. atwhere;
// The user changes from visitor status to login member, or vice versa
Drfind ["visitorname"] = username;
Drfind ["visitorlevel"] = 1;
}
Else
{
// None; add online information about me
DataRow drNew = dtOnline. NewRow ();
DrNew ["SessionID"] = sessionId;
DrNew ["VisitorName"] = USERNAME;
DrNew ["VisitorLevel"] = 1;
DrNew ["LastActiveTime"] = DateTime. Now;
DrNew ["LoginTime"] = DateTime. Now;
DrNew ["VisitorIP"] = System. Web. HttpContext. Current. Request. UserHostAddress;
DrNew ["UserWhere"] = this. AtWhere;

DtOnline. Rows. Add (drNew );
}
// If no one is performing deletion and the interval between the last deletion exceeds the set value

TimeSpan tsRemove = DateTime. Now-Convert. ToDateTime (GetApplication ("LastRemove "));
If (tsRemove. Seconds> REMOVEDIFF & this. GetApplication ("Removing"). ToString () = "n ")
{
// Locked. I am deleting expired users
System. Web. HttpContext. Current. Application. Lock ();
SetApplication ("Removing", "y ");
System. Web. HttpContext. Current. Application. UnLock ();
// Do not know whether Rows. Count decreases with the cycle. If it is more efficient than foreach, and foreach cannot be used here
For (int I = 0; I <dtOnline. Rows. Count; I ++)
{
DataRow drDel = dtOnline. Rows [I];
TimeSpan ts = DateTime. Now-Convert. ToDateTime (drDel ["LastActiveTime"]);
If (ts. Minutes> OFFLINEDIFF)
{
DtOnline. Rows. Remove (drDel );
System. Web. HttpContext. Current. Response. Write ("tired ");
}
}

System. Web. HttpContext. Current. Application. Lock ();
SetApplication ("Removing", "n ");
SetApplication ("LastRemove", DateTime. Now. ToString ());
System. Web. HttpContext. Current. Application. UnLock ();
}

// Put the data table that has been exhausted into the Application
DtOnline. AcceptChanges ();
System. Web. HttpContext. Current. Application. Lock ();
System. Web. HttpContext. Current. Application ["DataTalbeOnline"] = dtOnline;
System. Web. HttpContext. Current. Application. UnLock ();
}
// Obtain the corresponding value based on apcname
Private string GetApplication (string apcname)
{
Return System. Web. HttpContext. Current. Application [apcname]. ToString ();
}
// Assign a value to the Application
Public void SetApplication (string apcname, string apcvalue)
{

System. Web. HttpContext. Current. Application [apcname] = apcvalue;

}
/// <Summary>
/// Return the current user location
/// </Summary>
Private string AtWhere
{
Get
{
System. Text. StringBuilder sb = new System. Text. StringBuilder ();
Sb. Append (System. Web. HttpContext. Current. Request. ServerVariables ["url"]);
If (System. Web. HttpContext. Current. Request. ServerVariables ["QUERY_STRING"]! = String. Empty)
{
Sb. Append ("? ");
Sb. Append (System. Web. HttpContext. Current. Request. ServerVariables ["QUERY_STRING"]);
}
Return sb. ToString ();
}
}
/// <Summary>
/// Create a table
/// </Summary>
Public void CashTableInit ()
{
DataTable dt = new DataTable ("OnlineTalbe"); // create a temporary table
Dt. Columns. Add ("SessionID", typeof (string ));
Dt. Columns. Add ("VisitorName", typeof (string ));
Dt. Columns. Add ("VisitorLevel", typeof (int ));
Dt. Columns. Add ("LastActiveTime", typeof (DateTime ));
Dt. Columns. Add ("LoginTime", typeof (DateTime ));
Dt. Columns. Add ("VisitorIP", typeof (string ));
Dt. Columns. Add ("UserWhere", typeof (string ));
Dt. Columns ["SessionID"]. Unique = true;
Dt. PrimaryKey = new DataColumn [] {dt. Columns ["SessionID"]};

System. Web. HttpContext. Current. Application ["OnlineTalbe"] = dt;
System. Web. HttpContext. Current. Application ["LastRemove"] = DateTime. Now. ToString ();
System. Web. HttpContext. Current. Application ["Removing"] = "n ";
}
}

 

Add

 

Protected void Page_Load (object sender, EventArgs e)
{

// Online test statistics
OnlineCount oc = new onlineCount ();
Oc. CheckOnline ();

}

 

Add

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
If (Application ["OnlineTalbe"]! = Null)
{

BindData ();

}

}

}

Void bindData ()
{
If (Application ["OnlineTalbe"]! = Null)
{
Datatable dt = (datatable) application ["onlinetalbe"];
Datalist1.datasource = DT;
Datalist1.databind ();

Int totalrecord = DT. Rows. count;
Aspnetpager1.recordcount = totalrecord;

DT. Dispose ();
}

}

Protected void aspnetpager1_pagechanged (Object sender, eventargs E)
{

Binddata ();

}

 

 

Related Article

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.