Detecting connection drop

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Detecting connection drop

viniciusccarvalho
Hi there! I've just started with atmosphere. Looking as an alternative to cometd.

I need to use stream to send the data to the client, its working great using grails so far, and the AtmosphereHandler.

The problem is that when the client closes the connection (close the browser window on chrome or just close the url stream in a java client)

I never get notified:

def onStateChange = { event ->

                if(event.isCancelled()){
                        timer.cancel()
                        println("Terminaded by user");
                }

I did enable the CometValve at tomcat but still no luck

Any ideas?

Regards
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection drop

viniciusccarvalho
Hi, Just an update on this, I've been struggling for 2 days now. Moved from grails to plain old servlets with atmosphere runtime.

My environment:
atmosphere-0.7.2
tomcat 7.0.5

Here's my config:

<Context>
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
<Loader delegate="true"/>
</Context>


<servlet>
                <servlet-name>Atmosphere</servlet-name>
                <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
                <init-param>
                        <param-name>org.atmosphere.useNative</param-name>
                        <param-value>true</param-value>
                </init-param>
                <load-on-startup>0</load-on-startup>
        </servlet>

        <servlet-mapping>
                <servlet-name>Atmosphere</servlet-name>
                <url-pattern>/async/*</url-pattern>
        </servlet-mapping>
public void onRequest(
                        AtmosphereResource<HttpServletRequest, HttpServletResponse> resource)
                        throws IOException {
                HttpServletRequest req = resource.getRequest();
                HttpServletResponse res = resource.getResponse();
                ((AtmosphereResourceImpl)resource).addEventListener(new AtmosphereResourceEventListener() {
                       
                        public void onThrowable(
                                        AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
                                // TODO Auto-generated method stub
                               
                        }
                       
                        public void onSuspend(
                                        AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
                                // TODO Auto-generated method stub
                               
                        }
                       
                        public void onResume(
                                        AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
                                // TODO Auto-generated method stub
                               
                        }
                       
                        public void onDisconnect(
                                        AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
                                System.out.println("At last!!!!");
                               
                        }
                       
                        public void onBroadcast(
                                        AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event) {
                                // TODO Auto-generated method stub
                               
                        }
                });
                System.out.println("STARTEDoc");
                res.setContentType("text/plain;charset=ISO-8859-1");
                res.addHeader("Cache-Control", "private");
                res.addHeader("Pragma", "no-cache");
                res.getWriter().write("asdaskdjasldjasldjalskdjaklsdjalskdjalskdjalskdjalksdjas");
                res.getWriter().flush();
                if (req.getMethod().equalsIgnoreCase("GET")){
                        resource.suspend();
                }
               
        }

        public void onStateChange(
                        AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event)
                        throws IOException {
                HttpServletRequest req = event.getResource().getRequest();
                HttpServletResponse res = event.getResource().getResponse();
                if(event.isCancelled()){
                        System.out.println("TERMINATED");
                }else if(event.isSuspended()){
                        System.out.println("SUSPENEDED");
                }
        }

Thing is, I never see the ondisconnect get called. I tried many stuff, may combinations of init-params. Tried it on GlassFish 3.1 as well. Nothing seems to be working.

I need to create a stream (forever response), but that stream will be generated by a timer (could be a scheduled broadcaster as well) that will poll some system resources. My main problem is that when the client closes the browser window I need a hook on how to cancel the timer that is consuming resources.

Best Regards
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection drop

Jeanfrancois Arcand-4
In reply to this post by viniciusccarvalho
Salut,

On 11-07-05 9:04 AM, viniciusccarvalho wrote:

> Hi there! I've just started with atmosphere. Looking as an alternative to
> cometd.
>
> I need to use stream to send the data to the client, its working great using
> grails so far, and the AtmosphereHandler.
>
> The problem is that when the client closes the connection (close the browser
> window on chrome or just close the url stream in a java client)
>
> I never get notified:
>
> def onStateChange = { event ->
>
> if(event.isCancelled()){
> timer.cancel()
> println("Terminaded by user");
> }
>
> I did enable the CometValve at tomcat but still no luck
>
> Any ideas?

You must use the NIO or APR connector in other to make it work. Which
version of Tomcat are you using?

Thanks

-- Jeanfrancois

>
> Regards
>
> --
> View this message in context: http://atmosphere-users-mailling-list.2493822.n2.nabble.com/Detecting-connection-drop-tp6549674p6549674.html
> Sent from the Atmosphere users mailling list mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Detecting connection drop

Jeanfrancois Arcand-4
In reply to this post by viniciusccarvalho
Salut,

that sound really strange...just tested both Tomcat and GlassFish 3.1
and they both detect when the connection get remotly closed. On
GlassFish you need to enable Comet BTW. On Tomcat you must use the NIO
or APR connector as the default connector isn't able to detect
disconnection.

Thanks!

-- Jeanfrancois

On 11-07-05 12:23 PM, viniciusccarvalho wrote:

> Hi, Just an update on this, I've been struggling for 2 days now. Moved from
> grails to plain old servlets with atmosphere runtime.
>
> My environment:
> atmosphere-0.7.2
> tomcat 7.0.5
>
> Here's my config:
>
> <Context>
> <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
> <Loader delegate="true"/>
> </Context>
>
>
> <servlet>
> <servlet-name>Atmosphere</servlet-name>
> <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
> <init-param>
> <param-name>org.atmosphere.useNative</param-name>
> <param-value>true</param-value>
> </init-param>
> <load-on-startup>0</load-on-startup>
> </servlet>
>
> <servlet-mapping>
> <servlet-name>Atmosphere</servlet-name>
> <url-pattern>/async/*</url-pattern>
> </servlet-mapping>
> public void onRequest(
> AtmosphereResource&lt;HttpServletRequest, HttpServletResponse&gt;
> resource)
> throws IOException {
> HttpServletRequest req = resource.getRequest();
> HttpServletResponse res = resource.getResponse();
> ((AtmosphereResourceImpl)resource).addEventListener(new
> AtmosphereResourceEventListener() {
>
> public void onThrowable(
> AtmosphereResourceEvent&lt;HttpServletRequest, HttpServletResponse&gt;
> event) {
> // TODO Auto-generated method stub
>
> }
>
> public void onSuspend(
> AtmosphereResourceEvent&lt;HttpServletRequest, HttpServletResponse&gt;
> event) {
> // TODO Auto-generated method stub
>
> }
>
> public void onResume(
> AtmosphereResourceEvent&lt;HttpServletRequest, HttpServletResponse&gt;
> event) {
> // TODO Auto-generated method stub
>
> }
>
> public void onDisconnect(
> AtmosphereResourceEvent&lt;HttpServletRequest, HttpServletResponse&gt;
> event) {
> System.out.println("At last!!!!");
>
> }
>
> public void onBroadcast(
> AtmosphereResourceEvent&lt;HttpServletRequest, HttpServletResponse&gt;
> event) {
> // TODO Auto-generated method stub
>
> }
> });
> System.out.println("STARTEDoc");
> res.setContentType("text/plain;charset=ISO-8859-1");
> res.addHeader("Cache-Control", "private");
> res.addHeader("Pragma", "no-cache");
>
> res.getWriter().write("asdaskdjasldjasldjalskdjaklsdjalskdjalskdjalskdjalksdjas");
> res.getWriter().flush();
> if (req.getMethod().equalsIgnoreCase("GET")){
> resource.suspend();
> }
>
> }
>
> public void onStateChange(
> AtmosphereResourceEvent&lt;HttpServletRequest, HttpServletResponse&gt;
> event)
> throws IOException {
> HttpServletRequest req = event.getResource().getRequest();
> HttpServletResponse res = event.getResource().getResponse();
> if(event.isCancelled()){
> System.out.println("TERMINATED");
> }else if(event.isSuspended()){
> System.out.println("SUSPENEDED");
> }
> }
>
> Thing is, I never see the ondisconnect get called. I tried many stuff, may
> combinations of init-params. Tried it on GlassFish 3.1 as well. Nothing
> seems to be working.
>
> I need to create a stream (forever response), but that stream will be
> generated by a timer (could be a scheduled broadcaster as well) that will
> poll some system resources. My main problem is that when the client closes
> the browser window I need a hook on how to cancel the timer that is
> consuming resources.
>
> Best Regards
>
> --
> View this message in context: http://atmosphere-users-mailling-list.2493822.n2.nabble.com/Detecting-connection-drop-tp6549674p6550514.html
> Sent from the Atmosphere users mailling list mailing list archive at Nabble.com.
>