user2295633
user2295633

Reputation:

SSE Client Stops Working Anytime I Modify the Server (Server Sent Event)

I have a very simple SSE (Server Sent Event of Html5) with Java used on the server side. I have included both the client and the server below. The problem is that if I change the server side, even a simple change such as changing the text in a debug output, which has nothing to do with the actual text sent to the server, the client stops posting the received messages. Based on the output message being printed out on the server side, and on the request traffic that I see both in Firebug, and in Chrome's developer tool, I can see that the server is sending messages, and the browser receives the message. The only problem is that the JavaScript function that is supposed to be triggered when SSE is received is not being triggered. Any suggestion, or do you see anything wrong with my code?

<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>

<script>
    if(typeof(EventSource)!=="undefined")
    {
      var source=new EventSource("/ServerSideForSSE/sse3");
      source.onmessage=function(event)
      {
            document.getElementById("result").innerHTML+=event.data + "<br>";
      };
    }
    else
    {
      document.getElementById("result").innerHTML= 
"Sorry, your browser does not support server-sent events...";
    }
</script>

</body>
</html>

package com.ultratech;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


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


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

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/event-stream");  


          PrintWriter out = response.getWriter(); 
          int x = 0;
          while (x < 100) 
          {
              x++;
              Date currentTime_1 = new Date();        
              out.println("Current Time is: " + currentTime_1);     
              out.println();  
              out.flush();
              System.out.println("Another message sent to the cleint ...");
              if (x > 100) {System.exit(0);}
              try {
                Thread.sleep(5 * 1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          }//End of for().        
          out.close();                  
    }//End of doGet().

}

Upvotes: 1

Views: 709

Answers (1)

Mehrdad Rezai
Mehrdad Rezai

Reputation: 26

Each message that you send MUST start with the string "data:" (not including the quotes), and must be followed with a blank line.

If you don't follow this syntax, then the client doesn't consider what you send as a message, and thus the function is not triggered.

Upvotes: 1

Related Questions