Recently there is right for me in the blog above the note how many visitors currently online with me. The thing is very simplistic, should be relatively accurate. Who would like to have more background can watch at Klaus Bock posting this.
Klaus goes further. He registered a Java script on the client at the close of the browser a page calls that does nothing more than to finish the session on the server. But after 20 minutes the session on the server is either way finished with Session_End.
The implementation in BlogEngine.NET short BE similar to the following expires.
BE turned off by default the sessions. I switched simply return it in the Web.config.
< sessionState mode = "InProc" cookieName=".geniali.ch"/ >
Next must be when a new session starts and when a session is terminated noticed yet. There are two events that need to implement in the global.asax file in ASP.NET. Be the two not are still in there.
void Session_Start (object sender, EventArgs e)
{ / / User counter increase. Application.Lock(); Application ["Viewer"] = (int) application ["Viewer"] + 1; Application.UnLock(); }
void Session_End (object sender, EventArgs e) { / / Reduce user counter. Application.Lock(); If((int)Application["Viewers"] > 0) { Application ["Viewer"] = (int) application ["Viewer"] - 1; } Application.UnLock(); }
Application ["Viewer"] object must be initialized first yet. In addition it must be also ensured that this variable again called on startup of the Web to 0. To do this in the Application_Start event.
void Application_Start (object sender, EventArgs e)
{ / / Generate key of Viewer in the application Application ["Viewer"] = 0; ...
After that must be made only safe also display the "Viewer". To create a new UserControl. I have filed the UserControl under my theme in the controls folder.
UserOnline.ascx
"< % @ Control Language="C# "
AutoEventWireup = "true" CodeFile = "UserOnline.ascx.cs" inherits = "themes_GENiALi_Controls_UserOnline" % > < asp: literal ID = "litUsers" runat = "server" > < / asp: literal >
UserOnline.ascx.cs
using System;
using System.Collections.Generic; using System.LINQ; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
public partial class themes_GENiALi_Controls_UserOnline: System.Web.UI.UserControl { protected void Page_Load (object sender, EventArgs e) { litUsers.Text Application["Viewers"].ToString() = + "Visitors online"; } }
Can simply drag and place the UserControl on the site.master of theme. CSS still formatted and aligned the display.
It should look something like the bottom line. :-)