使用浏览器在Chrome中实时查看EventSource已被弃用? [英] Has viewing an EventSource in Real-Time in Chrome using the browser been deprecated?

查看:281
本文介绍了使用浏览器在Chrome中实时查看EventSource已被弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

去年(大约六月份),我撰写了关于HTML5事件源的内部操作指南,其中展示了浏览器在Chrome浏览器中查看事件流数据并实时更新。输出与此类似:

 事件:my-event-name 
Data:{my-data }

事件:my-event-name
Data:{my-data}

事件:my-event-name
Data: {my-data}

现在,当我在最新版Chrome中打开它时,作品(但读取事件源的应用程序仍然有效)。即当我浏览到EventSource URL时,我会看到一个空白屏幕,但不会显示或更新任何事件信息。



看起来这个功能在Chrome中已被弃用。

我可以看到的唯一相关的评论是 comment b
$ b


通常,内容不会在请求未完成时显示。

这看起来不符合EventSource开发人员的需求。



我的问题是:使用浏览器在Chrome浏览器中实时查看EventSource已被弃用?






编辑:这是一些复制此问题的Java代码。您设置maven项目并运行 Jetty.java ,然后浏览到http:// localhost:8070 /



症状是Chrome会暂停大约十秒钟(大约与循环中的睡眠时间相同),然后在完成后显示内容。即Chrome正在等待流完成,而不是像过去那样实时显示它。



以下是 pom.xml

 < project xmlns =http://maven.apache.org/POM/4.0.0xmlns: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>
<依赖关系>
< dependency>
< groupId> org.mortbay.jetty< / groupId>
< artifactId> jetty< / artifactId>
< version> 4.2.12< / version>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> servlet-api< / artifactId>
< version> 2.5< / version>
< /依赖关系>
< /依赖关系>
< / project>

这是 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();
$ b $ catch(Exception ex){
Logger.getLogger(Jetty.class.getName()).log(Level.SEVERE,null,ex);
}

}
}

这里是 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 {
//内容类型必须设置为text / event-stream
httpServletResponse.setContentType(text / event-stream);

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


解决方案

否,看起来很好。我只是在Chrome 34上做了一个EventSource站点的测试。(并且检查它实际上是使用EventSource,而不是其中一个。)



不知道其他的东西你的系统,我的猜测是服务器(或某个中介)正在缓冲内容,而不是之前。有时候,一个相当天真的配置更改,以小优化的名义,可以做到这一点。



顺便说一句,写下请求未显示但未完成 XHR和EventSource。然而,Chrome浏览器(即使是Chrome 20,IIRC)也是这种浏览器中的一种,它在流式传输时也提供XHR内容,而不是等待连接关闭,因此他们也必须考虑另一个浏览器或其他内容。




更新:查看您的代码,我期望您需要 writer.flush() writer.write(...)行之后。但是,如果需要这样做,您会在所有浏览器中看到相同的问题,而不仅仅是Chrome。 (即在其他浏览器中看到的问题非常重要。)


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();
}
}

解决方案

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.)

这篇关于使用浏览器在Chrome中实时查看EventSource已被弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆