Solved

Server side even on browser window close using ASP.NET & C#

Posted on 2010-08-13
8
776 Views
Last Modified: 2012-05-10
Hi All,

I've just developed an chat application which works perfectly except for one thing. I would like to show the clients who's online and show's not, now showing online is easy. I just make IsOnline in the database true. And when the user clicks logout it's easy once again cause I do the same except false instead of true. But the problem comes in when the user closes the browser window I have no way of know if that user is offline, because I can't execute a server side event. I've tried overriding OnUnload in code behind, but the problem is when the user goes to a different page it executes that method which I don't want. So what I've figured is that asp calls the OnUnload event everytime the client leaves that page.

Any help would be appreciated,
Thanks
Sean
0
Comment
Question by:SeanNij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 33427658
You can design a script where every X seconds of interval, it sends a request to your web application (this can be done by using AJAX, etc), and that tells the end user is still "alive".

In your request sent to your web application, try stored the "last request date" field in your table, so that we will not when was the last time the end user "ping" to your web application.

So, if the "last request date" is exceeded Y seconds, we categorized it as "offline".


Hope this approach make sense.
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 33427700
You can use
Response.IsClientConnected

to check that if the client is still connected to the server and not closed his browser.
0
 
LVL 1

Author Comment

by:SeanNij
ID: 33427787
Thanks Ryancys

Was a great idea, but the problem I have with this is every time the timer ticks the page stands still which means that while the timer does it's business the whole page stops and I don't want that, because the user is busy chatting there. If he's busy typing "Hello How Are you?" while the timer is bust loading then it stops the user from typing in the middle of the sentance and will come out something like "Hel re you?". Very annoying, so can't use that solution, but thanks anyway.

AjaySharmaaPJs

I don't understand how that would work. If the user closes the browser my web application disappears, so how can I execute a method on a web page when it's not even open anymore? Maybe I'm understanding wrong, but that's how I see it...

Thanks
S
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:akhileshcoer
ID: 33428131
alternatively, you can use javascript to do the job

With java script just check if browser is closing if yes then send a signal to server about the user status.
you can look at this http://codingforums.com/showthread.php?t=37279 


Hope this helps :)
0
 
LVL 28

Expert Comment

by:sybe
ID: 33428504
You need to define what IsOnline means.

If someone surfs to another page, is this person still online?
If some disconnects the cable by accident, and reconnects it after 5 minutes. Has this person been offline during that time? And how do you know the difference between a toilet visit and unplugging the cable?


> I just make IsOnline in the database true

It is easier to have IIS handle it. Use the Session object and more specifically the SessionOnStart() and SessionOnEnd() methods. As long as someone has a session, consider this person "online". If the person does not logout, the Session will time out eventually (usually 20 minutes).

You can also keep track of the time of the last activity of each session. So you can display a list of online users, and for each user list how many minutes ago the last activity was. Then everyone seeing that can decide for themselves if some who has been inactive for 10 minutes is still online or not.
0
 
LVL 1

Author Comment

by:SeanNij
ID: 33428601
Hi Sybe

IsOnline would be stating that the user is on the website no matter where... Redirecting to another page is still online (think of facebook)

Ok that's a great idea, but can I maybe ask if the user closes the browser and the session expires after lets say 20min, will the Session_Expire(object sender, EventArgs e) method in global.asax still fire even though the website has been closed down?

Thanks
Sean
0
 
LVL 28

Accepted Solution

by:
sybe earned 500 total points
ID: 33428689
> the user is on the website no matter where

That is not good enough.

> the session expires after lets say 20min, will the Session_Expire(object sender, EventArgs e) method in global.asax

Yes, because it runs on the server, not on the client. IIS is the keeper of the sessions, and IIS will expire sessions after 20 (or xx) minutes of inactivity. All the user needs to do is be inactive (away, shut down PC, burnt down the house or whatever reason there is to no longer request any new pages from your application).

0
 
LVL 1

Author Comment

by:SeanNij
ID: 33428762
Awesome!

Thanks for all the help.

Sean
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question