Code structure :
Server :
Java Servlet
Client : Simple
JSP
Communication : Server Sent Events every 1 second
Here is the problem.
My code needed the server to send updates every one second to the client as stated above. Hence, I added a while loop with a sleep of 1000 milliseconds in the servlet code as shown below. The following strange behavior is observed:
- While the server is sending updates to the client, and the client window closes by mistake, the server does not stop sending updates It continues sending the data.
- When the client is re-opened, it sends data much faster (almost double). For example, the server sends 60 seconds worth of updates (60 updates) in just 25-30 seconds. The server sends faster updates not only for this round of updates, but also for any subsequent updates.
On the other hand, when the servlet code is the default Server Sent Event code where server sends updates every 3 seconds and no while loop is involved, the server behaves as expected. Which is ::
- When the client window is closed while server is still sending updates, server stops sending the updates. The server resumes sending updates upon the client window being re-opened.
- Once, the window is re-opened, the rate which the data is being updated to the client is uniform : 3 seconds for a single update and 180 seconds for 60 updates.
The default Server Sent Events code for java where the update interval is 3 seconds is as shown below:
This server behavior is much unexpected. Am I writing the server side code wrong? I have looked around a lot and only found while loop method for modifying the server update interval. Is there any other method which I am missing? How can I overcome the above problem?
Thank you for your help in advance.