1

I'm using Jetty with Continuations to implement long polling, but I don't see a way to detect a client disconnecting.

I understand that in the time between the first run (creation of the Continuation) and any subsequent run there may be no traffic between the client and server due to the nature of HTTP, but I can settle with just being able to detect a client being disconnected upon trying to send a message back (server invoking the Continuation).

I've been trying to find a way of detecting that the writer wasn't actually writing to a client, but I can't seem to find one.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
Marcus
  • 778
  • 9
  • 20

1 Answers1

0

There is no way in java to know that a client has disconnected without trying to write to the connection and then catching the failure. My suggestion if you are trying to write your own long polling implementation would be to simply use cometd (cometd.org) as it has all of this completed already and gives you a full messaging semantic. If that doesn't work and you still need to write your own, then that project is a good example of how best to use continuations (or the servlet 3.0 async servlets).

jesse mcconnell
  • 7,102
  • 1
  • 22
  • 33
  • Everything about my implementation works as intended apart from detecting a disconnect. You say "trying to write to the connection and then catching the failure" would offer a means of detecting a disconnected client. This is the perfect solution for my case. Can you provide some code or at least the method which would throw an exception? – Marcus Apr 24 '13 at 14:44
  • Related [question](http://stackoverflow.com/questions/155243/why-is-it-impossible-without-attempting-i-o-to-detect-that-tcp-socket-was-grac) and [answer](http://stackoverflow.com/a/156403/775715) – Joakim Erdfelt Apr 24 '13 at 16:16
  • Thanks for the link @JoakimErdfelt , but the question relates to detecting the socket closure within Java directly and without doing any IO. In my situation it would be good enough to just detect a closed connection when I go to send a message back (actually doing some IO). – Marcus Apr 24 '13 at 17:20
  • Can you point me to the method that will cause a failure please @jesse-mcconnell ? – Marcus Apr 25 '13 at 14:03