Has viewing an EventSource in Real-Time in Chrome using the browser been deprecated?

心已入冬 提交于 2019-12-08 08:21:48

问题


Last year (approx June) I wrote an internal how-to on HTML5 Event Sources that showed viewing the event stream data in Chrome in the browser, updating in real-time. The output was similar to this:

Event: my-event-name
Data: {"my-data"}

Event: my-event-name
Data: {"my-data"}

Event: my-event-name
Data: {"my-data"}

Now when I open it in the latest version of Chrome this no longer works (but the application that reads the event source still works). ie I get a blank screen when I browse to the EventSource URL, no events information is shown or updated.

It would appear that this feature has been deprecated in Chrome.

The only vaguely related comment I could see on it was this comment:

Generally, content is not shown while request is not finished.

This appears not to address the needs of EventSource developers.

My question is: Has viewing an EventSource in Real-Time in Chrome using the browser been deprecated?


EDIT: Here is some Java code that replicates this issue. You setup the maven project and run Jetty.java and then browse to http:// localhost:8070/

The symptoms are that Chrome will pause for approx ten seconds (about the same time as the sleeps in the loop) and then will display the content when finished. ie Chrome is waiting until the stream finishes, rather than displaying it in real-time as it used to.

Here is pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.julian</groupId>
<artifactId>eventsource</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eventsource</name>
<dependencies>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty</artifactId>
        <version>4.2.12</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>
</project>

Here is Jetty.java

package jetty;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

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

import org.mortbay.http.SocketListener;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.ServletHttpContext;

public class Jetty {

  public static void main(String[] args) {
    try {
        Server server = new Server();
        SocketListener listener = new SocketListener();      

        listener.setHost("localhost");
        listener.setPort(8070);
        listener.setMinThreads(5);
        listener.setMaxThreads(250);
        server.addListener(listener);            

        ServletHttpContext context = (ServletHttpContext) server.getContext("/");
        context.addServlet("/", "jetty.HelloWorldServlet");

        server.start();
        server.join();

    } catch (Exception ex) {
        Logger.getLogger(Jetty.class.getName()).log(Level.SEVERE, null, ex);
    }

  }
} 

Here is HelloWorldServlet.java

package jetty;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) throws ServletException,
        IOException {
    // content type must be set to text/event-stream
    httpServletResponse.setContentType("text/event-stream");

    PrintWriter writer = httpServletResponse.getWriter();
    for (int i = 0; i < 10; i++) {
        writer.write("data: " + System.currentTimeMillis() + "\n\n");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    writer.close();
}
}

回答1:


No, it appears fine. I just did a test of an EventSource site on Chrome 34. (And checked it is actually using EventSource, and not one of the fallbacks.)

Not knowing anything else about your system, my guess would be that the server (or some intermediary) is now buffering content, and wasn't before. Sometimes a quite innocent config change, in the name of minor optimization, can do this.

BTW, whoever wrote the "not shown while request is not finished" was maybe confusing XHR and EventSource. However, Chrome (even as of Chrome 20, IIRC) is one of those browsers that also gives XHR content as it streams, not waiting for the connection to be closed, so they must also have been thinking about another browser, or something else.


UPDATE: Looking at your code, I would expect you need writer.flush() after the writer.write(...) line. However if that was needed you would have seen the same problem in all browsers, not just Chrome. (I.e. the question of what you see in other browsers is very important.)



来源:https://stackoverflow.com/questions/24115625/has-viewing-an-eventsource-in-real-time-in-chrome-using-the-browser-been-depreca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!