0

I am learning Server Sent events in java and for that I am using a simple example. I am using Windows 7, Java 1.7, Tomcat 7, Eclipse Indigo. I have created a servlet (SseServer.java), the code for this servlet is as follows:

package sse; 
import java.io.IOException; <br/>
import java.io.PrintWriter;<br/>
import java.util.Date;<br/>

import javax.servlet.ServletException;<br/>
import javax.servlet.annotation.WebServlet;<br/>
import javax.servlet.http.HttpServlet;<br/>
import javax.servlet.http.HttpServletRequest;<br/>
import javax.servlet.http.HttpServletResponse;<br/>

@WebServlet("/SseServer")<br/>
public class SseServer extends HttpServlet {<br/>
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // Besides "text/event-stream;", Chrome also needs charset, otherwise
          // does not work
          // "text/event-stream;charset=UTF-8"
          response.setContentType("text/event-stream;charset=UTF-8");
          response.setHeader("Cache-Control", "no-cache");
          response.setHeader("Connection", "keep-alive");

          PrintWriter out = response.getWriter();

          while (true) {
           out.print("id: " + "ServerTime" + "\n");
           out.print("data: " + new Date().toLocaleString() + "\n\n");
           out.flush();
           // out.close(); //Do not close the writer!
           try {
            Thread.currentThread().sleep(1000);
           } catch (InterruptedException e) {
            e.printStackTrace();
           }
          }

    }

}

And I am displaying the results in an html, SSE.html, the code for this is as shown below:

<!DOCTYPE html>
<html>
<body>
 <h1>Current Server Time  : </h1>

 <div id="ServerTime"></div>

 <script>
  if (typeof (EventSource) !== "undefined") {
   var source = new EventSource("http://localhost:8080/SSE/SseServer");
   
   // http://eastern1.j.layershift.co.uk
   //var source = new EventSource("http://eastern1.j.layershift.co.uk/SSE/SseServer");
   source.onmessage = function(event) {
    document.getElementById("ServerTime").innerHTML += event.data
      + "<br><br>";
   };
  } else {
   document.getElementById("ServerTime").innerHTML = "Sorry, your browser does not support server-sent events...";
  }
 </script>

</body>
</html>

When I run this code locally after every one second I am able to see the current time. I have also checked it on several browsers like chrome, firefox etc.

Since this code is working fine I decided to deploy this on cloud so I chose Jelastic.com. I created a war file and deployed it on Jelastic and tried running my sample application. But when I run the application from cloud, I can only see

Current Server Time :

I do not see the time. Can someone please tell me why this is happening? Is there something I need to change in my code? If yes then can someone please advice what it should be? Or should I change some other file/settings in eclipse while creating a war file?

Any help is much appreciated.

Abhijeet
  • 49
  • 10

1 Answers1

1

You had used absolute link, It's a bad practice. Try to use relative link. Your mistake was that link not corresponding to path on server

   var source = new EventSource("/SseServer");
DOBA
  • 61
  • 5
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Dec 15 '16 at 08:50
  • @DOBA You are a genius ! Thank you very much. I really appreciate your help. This works like a charm. I can't thank you enough. If possible please share your email id with me, so that I can keep in touch with you for future reference. – Abhijeet Dec 15 '16 at 08:57