Wednesday 13 March 2013

Count the Number of Online Visitors of an ASP.NET Web Site


Description:-        

                in this example we explain that how to get the number of user is online in our website.
you can also get the list of name of all online user or number of user.

this Example is Very useful when you Devlope a Website like Social website like Facebook,Twitter etc.. or also useful in any Application Bacause today every Website Provide a Chat Facility and Do this type of Functionality you have To use this program to Fetch the Number of user is online in You website.

Today All site provide a Fcaility for Chat to the other user so this is ver useful.

follow the following steps to get the number of online user in our site

First we have to need to enable sessionstate and configure its mode to InProc value in we.config file like:
        <system.web>
        <
sessionState mode="InProc" cookieless="false" timeout="20" />
        </
system.web>

where session mode In-process mode stores session state values and variables in memory on the local Web server.

A timeout value defines that how long your sessions are kept 'alive' - in other words here we set how long our users can be inactive before considered Offline. Here timeout value is in minitue like 20 minitue your session is alive after that it will automatically expired.


To show the number of online visitors/users on your ASPX page you can wite this code:
Online Visitors: 
<%Application["OnlineUsers"].ToString() %>

Next you can put this snippet code in you web UserControl, or inside Asp.Net AJAX UpdatePanel control so it will not load whole pages same like as we are seen any many social networkin site like Facebook.

Next use a Timer control to refresh it in regular intervals without refreshing the whole page or you can write the following line inside a HTML body tag. It will refresh your Page every 5 minutes.
<meta http-equiv="Refresh" content="300">

Here you have to use Global.asax File in which we Define four Event are as Follow

Application_Start:-

                        It is Called when your Application is Running at First Time.

Application_End:-

                        It is Called when you end or close the Current Application.

Session_Start:-

                        It is Called when the user is Login in Application and Create a session.
It is Generally Called when a New Session is start.

Session_End:-

                        It is Called when the user is Logout in Application and Destroy the session.
It is Generally Called when a Session is Remove or Deleted is start.

Export Gridview Row to PDF file Grid Data to PDF 

Rotate the Add without refreshing the webpage Rotate the Advertisement in Asp.net



Global.asax:-

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>

<script RunAt="server">

    List<string> list;
    void Application_Start(object sender, EventArgs e)
    {

        Application["OnlineUsers"] = 0;

        var list = new List<string>();
        Application["onlineusername"] = list;
        Application["chat"] = null;


    }

    void Session_Start(object sender, EventArgs e)
    {
        Application.Lock();
        List<string> list;
        if (Application["onlineusername"] != null)
        {
            list = (List<string>)Application["onlineusername"];
        }
        else
        {
            list = new List<string>();
            list.Add(Session["unm"].ToString());
        }
        Application["onlineusername"] = list;

        Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;





        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e)
    {
        Application.Lock();


        Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;

        list.Remove(Session["unm"].ToString());
        Application["onlineusername"] = list;
        Application.UnLock();
    }
   

</script>


0 comments:

Post a Comment