Long polling in java
Asked Answered
G

1

5

I have written my server side code for long polling. I want to write the client program in java. So as per long polling, the client sends a request which is help by the server and the server responds to the request when an event occurs and then client sends a new request.

So the trouble I am facing is with the client side which is to be written in java. After I send the request, how to keep checking in the client side if the server has responded to it or not. At what intervals should I keep sending the request to the server. I am totally confused. I am quite a beginner to Web technologies. I tried googling about this but all the examples are based on client side being a java script or JSP. Could anyone give a link to a proper tutorial with client side being a java HTTp application or provide an example on this (ie to achieve long polling).

Goldenrod answered 23/7, 2013 at 5:44 Comment(2)
Have you seen this? #3250179Rosenthal
Got my doubt cleared. Thanks for the help @ClydeByrdIIIGoldenrod
S
7

The call to HTTPURLConnection's getInputStream gives back a blocking stream. Calling a read on this stream will block the thread till data is available from the server, you need not keep polling for data.

Call the read in a separate thread and keep the HTTPURLConnection in scope without closing the connection. This should get you back the data nwhen available. Once you receive a 200OK from the server, send back another request on the same connection to keep it open. (This is if you have not implemented a request timeout.)

Statfarad answered 23/7, 2013 at 6:39 Comment(8)
okay one more clarification . How to resend a request on the same connection :| . I have been trying this for a long time.Goldenrod
Hope your server is not closing the connection once the response is sent? If it is then U will have to connect again. What is the exception you are getting?Statfarad
okay here is what I have Done. I am not reading the input stream on a separate thread as of now. for testing its all on the main thread. its just that everything is inside a loop. I start a connection and then I start my loop. I check the response code if its 200 i read. if its something else i have to resend the request. instead of resending a request it goes in a infinite loop :| .Goldenrod
check out the sample code I have at rsankarx.wordpress.com/2013/07/25/http_server_long_polling. Not sure what you might be doing wrong unless I see the code.Statfarad
Below is the link to my other question with the code. Though I got my program running :) by taking the advice of the first two commentators, it would be nice of you to suggest your opinion on the code or a better method to achieve it :). #17826607Goldenrod
@Statfarad "Hope your server is not closing the connection once the response is sent?" - if the server does not close the connection, then why does the client need to do long poll ?Slipstream
it connects once, and server pushes him the messages. or maybe i got something wrong?Slipstream
How is it implement in Android?Mythological

© 2022 - 2024 — McMap. All rights reserved.