Online user detection in ASPNET (using daemon threads)

Source: Internet
Author: User
Tags bool current time datetime thread thread class time interval
Backstage | Online below is my class file,

Online.cs (User online inspection)
/* 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, create a daemon thread that runs at regular intervals and then determines whether the time interval in all current user lists exceeds the
The specified time, if exceeded, the user is removed from the online list so that they can detect if the user is online, and if the individual
Write a user off-line processing, you can solve a lot of people asked: the user accidentally puppet after processing.

*/

#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;


Defines a structure
public struct User
{
public string name;
Public DateTime Lasttime;
Public DateTime Curtime;
public string SessionID;
public string Iswhere;
}

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

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

Public Onlineuser ()//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 + +)
{
Loops to determine if a user already exists
Soholife.user Tempuser = (soholife.user) _alluser[i];

if (Tempuser.sessionid.Equals (User.sessionid) && tempuser.name.Equals (User.Name))
{
return (false); 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 + +)
{
Loops to determine if a user already exists
Soholife.user Tempuser = (soholife.user) _alluser[i];
if (tempuser.name.Equals (name))
{
return (true);
}
}
return (false);
}
}

Function Description: Update user online time
Return value: Latest Online list of users
Public Boolean Checkuseronline (string name)
{
You need to first determine if the user is already in the list of users
if (_alluser!=null)
{
for (int i = 0; i < _alluser. Count; i + +)
{
Soholife.user Tempuser = (soholife.user) _alluser[i];
First determine whether the current user is himself
if (tempuser.name.Equals (name))
{
Update users online time
Tempuser.curtime=datetime.now;
Alluser[i]=tempuser;
return (true);
}
}
}
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 questions, finally decided to give up and use the existing format, however, just from happy there is a single piece have a knowledge of the night back
will use it again to write another pattern)
*/
public class Checkonline
{
const int delay_times = 5000; Define execution time interval of 5 seconds
const int delay_seconds=30; 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)
{
Soholife.onlineuser temp=new Soholife.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 80 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);
}
}
Thread.Sleep (Delay_times);

}
}
}
}





The compiled statement is: csc/t:library/out:. /bin/online.dll/r:system.dll Online.cs

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.