views:

532

answers:

3

In my application, the client is a Javascript set of functions in the browser, and it does some work - for example, playing a clip. It uses XmlHttpRequest to talk to the server. However, the server is allowed to abruptly close the connection since there is no other way it seems, to interrupt the client. Can the client detect, while it is playing the clip, that the connection was closed, and so print a message and erase the page? Any help appreciated. thanks, Anil

A: 

You can probably poll the XmlHttpRequest object, so just try to send a heartbeat every once in a while to see if the connection is closed. Otherwise, the server would have to send some signal to tell the client it is going to close the connection.

CookieOfFortune
thanks for replying. but Javascript is single threaded. Is there some sort of exception or callback that can be registered for?What I want to do is, wipe the browser clean (erase) when the connection is closed.
yes, you can create timing events in Javascript. http://www.devshed.com/c/a/JavaScript/Using-Timers-in-JavaScript/
CookieOfFortune
+1  A: 

If the clip is streamed to the client, you could just stop serving it.

However, it seems like the clip is being downloaded and then played through the browser. In this instance it's probably best to use a watchdog approach as described by CookieOfFortune: Poll the server regularly (once a second or so) and get it to respond with tiny message of confirmation. When the connection is closed, get the server to respond with a negative messgage.

Unfortunately, without using a comet-like system, it's very hard to get the server to 'send' a message indicating session closure.

Bear in mind though, that as soon as the client has downloaded a clip they will be able to play it in full if they want to. Unfortunately there's no way to stop this besides switching to a streaming approach. If securing your content is a priority, I'd suggest making this change.

Tom Wright
AFAIK, Javascript is single threaded. Is there some sort of exception or callback that can be registered for? What I want to do is, wipe the browser clean (erase) when the connection is closed.Perhaps a session id will work. I shall try that and post back.
A: 

It does seem that the server cannot notify the client that the connection is closed; however the polling method suggested is not as efficient as a notification would have been.

I solved it by specifying that at the NEXT Get request, the client would be told that its session is invalid.

This was implemented by URL rewriting - appending "jsessionid=[id]" on each request sent by the Javascript functions. the servlet stores the current session id.

It is a suboptimal solution.

Thanks for the suggestions.

Anil