Solved

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

Posted on 2010-08-13
8
732 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
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 4

Expert Comment

by:akhileshcoer
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 28

Expert Comment

by:sybe
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
Awesome!

Thanks for all the help.

Sean
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now