Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JS EventSource从我的3.0异步servlet接收到神秘错误_Java_Javascript_Server Sent Events - Fatal编程技术网

Java JS EventSource从我的3.0异步servlet接收到神秘错误

Java JS EventSource从我的3.0异步servlet接收到神秘错误,java,javascript,server-sent-events,Java,Javascript,Server Sent Events,我的EventSource连接后,输出会被打印出来,但是当我调用asyncContext.complete()时,我的JS EventSource收到一个错误EventSource的错误从不包含消息。 对于那些投票决定关闭的人:如果下次你能提供一个理由就好了我甚至发布了Git项目的链接。一个人还能做什么?o、 o 使用Tomcat 7.0.30、7.0.59和8.0.20进行测试 响应返回http状态OK/200和以下标题: 连接:关闭 内容类型:文本/事件流;字符集=UTF-8 日期:201

我的EventSource连接后,输出会被打印出来,但是当我调用asyncContext.complete()时,我的JS EventSource收到一个错误EventSource的错误从不包含消息。

对于那些投票决定关闭的人:如果下次你能提供一个理由就好了我甚至发布了Git项目的链接。一个人还能做什么?o、 o

使用Tomcat 7.0.30、7.0.59和8.0.20进行测试

响应返回http状态OK/200和以下标题:

  • 连接:关闭
  • 内容类型:文本/事件流;字符集=UTF-8
  • 日期:2015年2月28日星期六21:32:35 GMT
  • 服务器:ApacheCoote/1.1
  • 传输编码:标识
如果有人愿意尝试,以下是最小化的Maven项目:

我尝试过关闭防病毒软件之类的方法

你能看到这里有什么可疑的东西吗

JS:

爪哇:

@WebServlet(urlPatterns={“/sse”},asyncSupported=true)
公共类EventSource2扩展了HttpServlet{
私有静态ScheduledExecutorService executor=执行者
.newScheduledThreadPool(10);
@凌驾
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出IOException、ServletException{
response.setContentType(“文本/事件流”);
响应。setCharacterEncoding(“UTF-8”);
final AsyncContext AsyncContext=request.startAsync();
Runnable runnableDummy=new Runnable(){
@凌驾
公开募捐{
试一试{
PrintWriter writer=asyncContext.getResponse().getWriter();
对于(int i=0;i<5;i++){
睡眠(1000);
writer.println(“数据:Hello”+i+“\n”);
writer.flush();
}
}捕获(IOE异常){
抛出新的RuntimeException(e.getMessage(),e);
}捕捉(中断异常e){
抛出新的RuntimeException(e.getMessage(),e);
}
asyncContext.complete();
}
};
执行人。执行(runnableDummy);
}
@凌驾
公共空间销毁(){
executor.shutdown();
}
}
server.xml

<Connector port="80" protocol="HTTP/1.1"
    URIEncoding="UTF-8" connectionTimeout="20000" asyncTimeout="20000" />


“我的JS EventSource接收到一个错误”,那个错误是什么?我不知道,tt没有说。我知道,很奇怪,但事件对象没有消息。而DevTools中的网络窗格并不表示有任何错误。
@WebServlet(urlPatterns = { "/sse" }, asyncSupported = true)
public class EventSource2 extends HttpServlet {
    private static ScheduledExecutorService executor = Executors
        .newScheduledThreadPool(10);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException {
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");

        final AsyncContext asyncContext = request.startAsync();

        Runnable runnableDummy = new Runnable() {
            @Override
            public void run() {
                try {
                    PrintWriter writer = asyncContext.getResponse().getWriter();
                    for (int i = 0; i < 5; i++) {
                        Thread.sleep(1000);
                        writer.println("data: Hello " + i + "\n");
                        writer.flush();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage(), e);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e.getMessage(), e);
                }

                asyncContext.complete();
            }
        };

        executor.execute(runnableDummy);
    }

    @Override
    public void destroy() {
        executor.shutdown();
    }
}
<Connector port="80" protocol="HTTP/1.1"
    URIEncoding="UTF-8" connectionTimeout="20000" asyncTimeout="20000" />