Java JS EventSource从我的3.0异步servlet接收到神秘错误
我的EventSource连接后,输出会被打印出来,但是当我调用asyncContext.complete()时,我的JS EventSource收到一个错误EventSource的错误从不包含消息。 对于那些投票决定关闭的人:如果下次你能提供一个理由就好了我甚至发布了Git项目的链接。一个人还能做什么?o、 o 使用Tomcat 7.0.30、7.0.59和8.0.20进行测试 响应返回http状态OK/200和以下标题: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
- 连接:关闭
- 内容类型:文本/事件流;字符集=UTF-8
- 日期:2015年2月28日星期六21:32:35 GMT
- 服务器:ApacheCoote/1.1
- 传输编码:标识
@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" />