How to prevent net::ERR_INCOMPLETE_CHUNKED_ENCODING when using HTML5 Server events and Java Servlets?

后端 未结 2 1952
死守一世寂寞
死守一世寂寞 2021-02-20 10:31

i just started to play around with Server Events and i run into a chrome error message i would like to understand. i searched the web real quick but didn\'t find an explanation

相关标签:
2条回答
  • 2021-02-20 10:48

    Ok, so i couldn't stand still and look closer whats happening.

    The AsyncContext object has a setTimeout(...) method. Per default in my version of tomcat (Tomcat embedded 8) the value is set to 30,000 ms (30 seconds). That's exactly the duration after i got the net::ERR_INCOMPLETE_CHUNKED_ENCODING error in my chrome console.

    i checked using:

    System.out.println("Current Timeout is: " + asynCtx.getTimeout() + " ms");
    

    which showed:

    Current Timeout is: 30000 ms
    

    so to avoid the net:ERR message someone could set the timeout to 0. But than the event thread keeps running forever (unfortunately). Another solution, which i used, is to add a AsyncListener to the AsyncContext and call the complete() method inside the onTimeout() method.

    from the API doc of the complete() method:

    Completes the asynchronous operation that was started on the request that was used to initialze this AsyncContext, closing the response that was used to initialize this AsyncContext. Any listeners of type AsyncListener that were registered with the ServletRequest for which this AsyncContext was created will be invoked at their onComplete method.

    the source code of my listener:

    asynCtx.addListener(new AsyncListener()
    {
      @Override
      public void onComplete(AsyncEvent asyncEvent) throws IOException
      {
        System.out.println("onComplete(...)");
      }
    
      @Override
      public void onTimeout(AsyncEvent asyncEvent) throws IOException
      {
        // this will close the request and the context gracefully
        // and the net:ERR is gone.
        asyncEvent.getAsyncContext().complete();
        System.out.println("onTimeout(...)");
      }
    
      @Override
      public void onError(AsyncEvent asyncEvent) throws IOException
      {
        System.out.println("onError(...)");
      }
    
      @Override
      public void onStartAsync(AsyncEvent asyncEvent) throws IOException
      {
        System.out.println("onStart(...)");
      }
    });
    

    so yes, it was due lack of knowledge. i hope this is helpful for someone.

    0 讨论(0)
  • 2021-02-20 10:55

    You can send small "no operation" (noop) chunks to keep the connection alive every 10 seconds or so.

    0 讨论(0)
提交回复
热议问题