[2.2.2 java] read chunk by chunk for SSE data

118 views
Skip to first unread message

keepscoding

unread,
Jun 26, 2014, 4:18:32 AM6/26/14
to play-fr...@googlegroups.com
I want to receive a stream of SSE data from another server. I'll start connect to the server at the beginning when my play is running.

WS.url("http://localhost:9001/SSE").setHeader("Content-Type" -> "text/event-stream").get()......

how do i read the output chunk by chunk by spliting "data:" SSE header in Java?
WS seem like waiting for end of stream before it return...



Alberto Souza

unread,
Jun 26, 2014, 8:30:27 AM6/26/14
to play-fr...@googlegroups.com
Hi, i am just guessing here... But reading the Async-http-client(ning) documentation, i did not find any part related to sse support. I've found the websocket support which could be fit for your needs. Just for clarification, WS lib uses Ning under the hood to handle the async connections. You can check their github to find more information => https://github.com/AsyncHttpClient/async-http-client

keepscoding

unread,
Jun 26, 2014, 9:18:30 AM6/26/14
to play-fr...@googlegroups.com
Thanks for response. 
currently, play has SSE support for server side code in java and client from browser using javascript. But, my server side code now acting as a client to listen SSE from another another server. I'll check it out.. your recommmendation. 

keepscoding

unread,
Jun 26, 2014, 10:43:27 PM6/26/14
to play-fr...@googlegroups.com
I've tried it... it seem like no response. maybe i need to add some stuff before it work?

public static Result myNing() throws IOException {

AsyncHttpClient c = new AsyncHttpClient();
Future<String> f = c.prepareGet("http://localhost:9001/clock")
.addHeader("Content-Type", "text/event-stream")
.addHeader("Cache-Control" , "no-cache")
.execute(new AsyncHandler<String>() {
   private ByteArrayOutputStream bytes = new ByteArrayOutputStream();

   @Override
   public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
       int statusCode = status.getStatusCode();
       // The Status have been read
       // If you don't want to read the headers,body or stop processing the response
       if (statusCode >= 500) {
           return STATE.ABORT;
       }
return STATE.CONTINUE;
   }

   @Override
   public STATE onHeadersReceived(HttpResponseHeaders h) throws Exception {
       //Headers headers = h.getHeaders();
        // The headers have been read
        // If you don't want to read the body, or stop processing the response
        return STATE.ABORT;
   }

   @Override
   public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
        bytes.write(bodyPart.getBodyPartBytes());
        Logger.debug("test" + new String(bodyPart.getBodyPartBytes()));
        return STATE.CONTINUE;
   }

   @Override
   public String onCompleted() throws Exception {
        // Will be invoked once the response has been fully read or a ResponseComplete exception
        // has been thrown.
        // NOTE: should probably use Content-Encoding from headers
        return bytes.toString("UTF-8");
   }

   @Override
   public void onThrowable(Throwable t) {
   }
});
return ok("ning see");
}


On Thursday, June 26, 2014 8:30:27 PM UTC+8, Alberto Souza wrote:
Reply all
Reply to author
Forward
0 new messages