Asp. NET to achieve user online detection of the class source code

Source: Internet
Author: User
Tags bool count current time datetime thread thread class time interval tostring
asp.net| Online | Online detection//online.cs (user online detection)
/* Program Implementation ideas:

The user has several properties:
Name: User Name
SessionID: User ID, which uniquely represents a user
Iswhere: Additional information, where the user is currently located
Lasttime: User Login time
Curtime: This refresh time

In the client, use an IFRAME, load a refresh page, every xx seconds update his name corresponding to the curtime, it means that he is still in

On the server side, set up a daemon thread, run it at regular intervals, and then determine if the time interval in all current user lists exceeds the specified time, and if it is exceeded, remove the user from the online list so that you can detect if the user is online, and if you are alone
Write a user off-line processing, you can solve a lot of people asked: the user accidentally puppet after processing.
*/

#define DEBUG

Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Collections;
Using System.Threading;
Using System.Web;
Using System.Diagnostics;

Namespace Sohoproject
{
Defines a structure
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 property
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 ();
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.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);
}
}


Feature Description: Add current users to the online list
If the user's data is still in the online list, the user is temporarily not logged in, prompting the user to exist
public bool Addusertoonline (user user)
{
#if DEBUG
(New Sohoproject.sohodebug ()). Writetodoc ("Enter < add current users to the online list ...");
(New Sohoproject.sohodebug ()). Writetodoc ("\ r \ n");
#endif


Start the search to see if the user already exists, or change the data if it exists, or add a new user
String strexpr;
strexpr = "sessionid= '" + User.sessionid + "'";
Datarow[] 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 ("\ r \ n");
#endif


Curuser = _alluser. Select (strexpr);

#if DEBUG
(New Sohoproject.sohodebug ()). Writetodoc (strexpr);
(New Sohoproject.sohodebug ()). Writetodoc (CurUser.Length.ToString ());
#endif


if (Curuser.length >0)
{
for (int i = 0; i < curuser.length i + +)
{
curuser[i]["Curtime"]=datetime.now;
curuser[i]["Iswhere"]=user.iswhere;
}
}
Else
{
Add new data directly
DataRow Myrow;
Try
{
Myrow = _alluser. NewRow ();
Then Add the new row to the collection.
myrow["name"] = User.Name;
myrow["IP" = user.ip;
myrow["Iswhere"] = 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 Description: To determine whether a user online, this part of the temporary need not
Return value: True stands for online, false is not
Public Boolean Isuseronline (string name)
{
You need to first determine if the user is already in the list of users
Start the search to see if the user already exists, or change the data if it exists, or add a new user
String strexpr;
strexpr = "name = '" + name + "'";
Datarow[] Curuser;
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 list of users
Public Boolean checkuseronline (string name,string iswhere,string sessionid,string IP)
{
#if DEBUG
(New Sohoproject.sohodebug ()). Writetodoc ("Start to check user method ...");
(New Sohoproject.sohodebug ()). Writetodoc ("");
#endif

You need to first determine if the user is already in the list of users
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

return true;
}
}

Define Online User Classes
public class Onlineuser_old
{
private static ArrayList _alluser; Define User

Public ArrayList AllUser
{
Get{return _alluser;}
Set{_alluser=value;}
}

Public onlineuser_old ()//constructors
{
if (_alluser==null)
{
_alluser=new ArrayList ();
}
}

Feature Description: Add current users to the online list
If the user's data is still in the online list, the user is temporarily not logged in, prompting the user to exist
public bool Addusertoonline (user user)
{
You need to first determine if the user is already in the list of users
if (_alluser==null)
{
_alluser. ADD (user);
return (true);
}
Else
{
for (int i = 0; i < _alluser. Count; i + +)
{
The loop determines whether the user already exists sohoproject.user Tempuser = (sohoproject.user) _alluser[i];

if (Tempuser.sessionid.Equals (User.sessionid))
{
Update users 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); User already exists, exit directly}
}
_alluser. ADD (user);
return (true);
}
}

Function Description: To determine whether a user online, this part of the temporary need not
Return value: True stands for online, false is not
Public Boolean Isuseronline (string name)
{
You need to first determine if the user is already in the list of users
if (_alluser==null)
{
return (false);
}
Else
{
for (int i = 0; i < _alluser. Count; i + +)
{
The loop determines whether 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 list of users
Public Boolean checkuseronline (string name,string iswhere,string sessionid,string IP)
{
You need to first determine if the user is already in the list of users
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);
}
}



/*
The following starts to establish the Daemon thread class:
(Note: Here, when you start writing, you want to make a single mode, but because you have not done this before, so it happened.
A lot of problems, finally decided to give up and use the existing format)
*/
public class Checkonline
{
const int delay_times = 10000; Define execution time interval of 5 seconds
const int delay_seconds=60; Set user drop time to 30 seconds

private thread thread; Defining internal threads
private static bool _flag=false; Define unique Flags

Public Checkonline ()
{
if (!_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< temp.alluser.count;i++)
// {
User tmpuser= (user) temp.alluser[i];
I'm going to add the user's latest time to 30 seconds and then compare the current time to a small current time,
To indicate that the user is already puppet, delete his record
if (Tmpuser.curtime.AddSeconds (delay_seconds). CompareTo (DateTime.Now) <0)
// {
Temp.alluser.RemoveAt (i);
// }
// }



Sohoproject.onlineuser temp=new Sohoproject.onlineuser (); Define a User Object
Start checking to see if a user has expired string strexpr;
Tmpuser.curtime.AddSeconds (delay_seconds). CompareTo (DateTime.Now) <0
strexpr = "Curtime < '" + DateTime.Now.AddSeconds (0-delay_seconds) + "'";
#if DEBUG
(New Sohoproject.sohodebug ()). Writetodoc (strexpr);
#endif

Datarow[] Curuser;
Use the Select method to find all rows matching the filter.
Curuser = Temp.alluser.Select (strexpr);

if (Curuser.length >0)
{
Delete these records
for (int i = 0; i < curuser.length i + +)
{
Curuser[i]. Delete ();
}
Temp.alluser.AcceptChanges ();
}
Thread.Sleep (Delay_times);
}
}
}
}





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.