I am making a simple blog system but I don't know how to make "Who's online" section. I doesn't have to show the members names(if it could do that, what would be awesome). I just want it to count how many users are online at the moment. How can I do that?
create client timer event on your blog page. which will send a certain ping message to a webserver with identifier (like userid or auth cookie)
server method would receive all ping messages... based on identifier it would identify whether ping message is from new user or already present user... then increase the count and display that user as online... if ping message does not arrive after certain
interval (which is more than timer on client side) then decerease the count
hope this helps...
Cheers!
KK
Please mark as Answer if post helps in resolving your issue
My Site
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application("OnlineNow") = 0
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
Application.Lock()
Dim intUsers As Integer = CInt(Application("OnlineNow"))
intUsers += 1
Application("OnlineNow") = intUsers
Application.UnLock()
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
Application.Lock()
Dim intUsers As Integer = CInt(Application("OnlineNow"))
intUsers -= 1
Application("OnlineNow") = intUsers
Application.UnLock()
End Sub
Hi crouchie thanks for your simple code. But I am using C# and I had some problems when I tried to conver it to c#. WHen I wrote the codes it didn't see the "Application" secitons. It says "Application" is a namespace but it used like a method. or something
like that. How can I fix it?
Do you have a Global.asax file in your website? If not, add it. You will then see the Application sections like in my original reply. You can then place the code in.
Global.asax file will look like this:
<%@ Application Language="VB" %>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
</script>
These correspond to the code in my original post
---
When I wrote 'application' I meant within the website (master page for example). For some reason the site stripped out the 'p' tag surrounding it
Maybe I'm doing something wrong or maybe VB.net and c# files are different.(please check c# ones then you'll understand what I mean(you can use a vb to C# conversation page)) I added Global.asax into my application named "WebApplication1". But it still gives
the same error. the error exactly is "Appliacation(I tried to replace it with my applicaitons name) is a "namespace" but is used like a "variable". And wrote your last code into my page's(homepage.aspx) source section and it gave the same error. And in global
asax file I don't have <%@ Application Language="the language" %> or <script runat="server"> tags. I am sorry I am a beginner and thank you so MUCH for you help and your caring. I'Ll be so happy if you can
keep helping me with this error.
I just created a new project, added a Default.aspx and a Global Class to the project
Here is a copy/paste of the Global file:
<%@ Application Language="C#" %>
<script runat="server">
public void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["OnlineNow"] = 0;
}
public void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
public void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
public void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
int intUsers = Convert.ToInt32(Application["OnlineNow"]);
intUsers += 1;
Application["OnlineNow"] = intUsers;
Application.UnLock();
}
public void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Application.Lock();
int intUsers = Convert.ToInt32(Application["OnlineNow"]);
intUsers -= 1;
Application["OnlineNow"] = intUsers;
Application.UnLock();
}
</script>
Change the targetFramework to your version of the Framework
That will then work
---
You said you later wish to show the members that are online. The Membership Class has a built-in function for that but only when they are logged in (Membership.GetNumberOfUsersOnline().ToString();) and not guests. Remember to add this to the top:
Mac14
Member
46 Points
71 Posts
Who's online?
Dec 03, 2012 05:43 PM|LINK
Hello everyone!
I am making a simple blog system but I don't know how to make "Who's online" section. I doesn't have to show the members names(if it could do that, what would be awesome). I just want it to count how many users are online at the moment. How can I do that?
Thanks.
kedarrkulkar...
All-Star
35559 Points
5698 Posts
Re: Who's online?
Dec 03, 2012 06:00 PM|LINK
something like this should help
http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx
or there are multiple solutions...
create client timer event on your blog page. which will send a certain ping message to a webserver with identifier (like userid or auth cookie)
server method would receive all ping messages... based on identifier it would identify whether ping message is from new user or already present user... then increase the count and display that user as online... if ping message does not arrive after certain interval (which is more than timer on client side) then decerease the count
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
crouchie2004
Member
498 Points
308 Posts
Re: Who's online?
Dec 03, 2012 07:33 PM|LINK
In the Global.asax file
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs on application startup Application("OnlineNow") = 0 End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a new session is started Application.Lock() Dim intUsers As Integer = CInt(Application("OnlineNow")) intUsers += 1 Application("OnlineNow") = intUsers Application.UnLock() End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a session ends. ' Note: The Session_End event is raised only when the sessionstate mode ' is set to InProc in the Web.config file. If session mode is set to StateServer ' or SQLServer, the event is not raised. Application.Lock() Dim intUsers As Integer = CInt(Application("OnlineNow")) intUsers -= 1 Application("OnlineNow") = intUsers Application.UnLock() End SubThen in the application:
Online Now: <%= Application("OnlineNow").ToString()%>In the Web.config, it's set to 10 mins:
Hope this helps
Homepage | YouTube Channel | Post code,
not linksMac14
Member
46 Points
71 Posts
Re: Who's online?
Dec 04, 2012 03:41 PM|LINK
Hi crouchie thanks for your simple code. But I am using C# and I had some problems when I tried to conver it to c#. WHen I wrote the codes it didn't see the "Application" secitons. It says "Application" is a namespace but it used like a method. or something like that. How can I fix it?
Thanks
crouchie2004
Member
498 Points
308 Posts
Re: Who's online?
Dec 04, 2012 04:22 PM|LINK
Hi,
Do you have a Global.asax file in your website? If not, add it. You will then see the Application sections like in my original reply. You can then place the code in.
Global.asax file will look like this:
<%@ Application Language="VB" %> <script runat="server"> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) End Sub </script>These correspond to the code in my original post
---
When I wrote 'application' I meant within the website (master page for example). For some reason the site stripped out the 'p' tag surrounding it
<p>Online Now: <%= Application("OnlineNow").ToString()%></p>Should show for Example at runtime:
Online Now: 1
Homepage | YouTube Channel | Post code,
not linksMac14
Member
46 Points
71 Posts
Re: Who's online?
Dec 04, 2012 05:24 PM|LINK
Hi,
Maybe I'm doing something wrong or maybe VB.net and c# files are different.(please check c# ones then you'll understand what I mean(you can use a vb to C# conversation page)) I added Global.asax into my application named "WebApplication1". But it still gives the same error. the error exactly is "Appliacation(I tried to replace it with my applicaitons name) is a "namespace" but is used like a "variable". And wrote your last code into my page's(homepage.aspx) source section and it gave the same error. And in global asax file I don't have <%@ Application Language="the language" %> or <script runat="server"> tags. I am sorry I am a beginner and thank you so MUCH for you help and your caring. I'Ll be so happy if you can keep helping me with this error.
Waiting for your answer.
THANKS!
crouchie2004
Member
498 Points
308 Posts
Re: Who's online?
Dec 04, 2012 05:48 PM|LINK
Hi,
I just created a new project, added a Default.aspx and a Global Class to the project
Here is a copy/paste of the Global file:
<%@ Application Language="C#" %> <script runat="server"> public void Application_Start(object sender, EventArgs e) { // Code that runs on application startup Application["OnlineNow"] = 0; } public void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } public void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } public void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started Application.Lock(); int intUsers = Convert.ToInt32(Application["OnlineNow"]); intUsers += 1; Application["OnlineNow"] = intUsers; Application.UnLock(); } public void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. Application.Lock(); int intUsers = Convert.ToInt32(Application["OnlineNow"]); intUsers -= 1; Application["OnlineNow"] = intUsers; Application.UnLock(); } </script>Here is the code I put in the Default.aspx page:
Set 'debug="true" in the web.config file
<configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> </configuration>Change the targetFramework to your version of the Framework
That will then work
---
You said you later wish to show the members that are online. The Membership Class has a built-in function for that but only when they are logged in (Membership.GetNumberOfUsersOnline().ToString();) and not guests. Remember to add this to the top:
For the c# code: http://msdn.microsoft.com/en-us/library/system.web.security.membership.getnumberofusersonline.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
Homepage | YouTube Channel | Post code,
not linksMac14
Member
46 Points
71 Posts
Re: Who's online?
Dec 04, 2012 06:06 PM|LINK
IT WORKED NOW! Thank you so much for you help. :))) And I don't know why but not it accepts "Application" :)
Thanks again. :)
crouchie2004
Member
498 Points
308 Posts
Re: Who's online?
Dec 04, 2012 06:11 PM|LINK
Happy to help
The Application needed the [] rather than () I guess. If you got Object not set... error it means the session variable 'OnlineNow' was different
Good luck!!
Homepage | YouTube Channel | Post code,
not links